如何在 dart post 请求中发送数组

kar*_*sim 4 android dart flutter firebase-cloud-messaging

我尝试使用其registration_ids.

这是我的代码:

List<String> tokens=["token1","token2"];
final  url='https://fcm.googleapis.com/fcm/send';
 http.post(url,headers:{
  "Accept": "application/json",
  "Authorization":"key=mykey"
  ,"project_id":"proID"
},
body: 
  {
 "registration_ids" :tokens ,
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
 }
}
Run Code Online (Sandbox Code Playgroud)

当应用程序运行时,显示此错误:

发生异常。_CastError(类型“List”不是类型转换中类型“String”的子类型)

如何修复它?

kar*_*sim 5

问题解决了; 我刚刚对正文进行了编码:

List<String> tokens=["token1","token2"];
final  url='https://fcm.googleapis.com/fcm/send';
 http.post(url,headers:{
  "Accept": "application/json",
  "Authorization":"key=mykey"
  ,"project_id":"proID"
},
body:json.encode( 
  {
 "registration_ids" :tokens ,
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
 }
)
}
Run Code Online (Sandbox Code Playgroud)