如何在DART Post请求中发送数组

时间:2019-07-13 07:39:53

标签: android flutter dart firebase-cloud-messaging

我尝试使用其registration_id将消息发送到设备组 这是我的代码:

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"
 }
}

运行应用程序时出现此错误:

Exception has occurred.
_CastError (type 'List<String>' is not a subtype of type 'String' in type cast)

如何解决?

2 个答案:

答案 0 :(得分:0)

由于注册需要JSON字符串,您遇到了问题,但是您正在向其中传递列表对象。您可以通过将List<String>强制转换为String来解决此问题。

通过使用toString()方法转换令牌列表,您将获得一个类似"['token1','token2']"的字符串

这是修改后的代码:

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.toString() ,
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
 }
}

答案 1 :(得分:0)

问题解决了,我只是编码了身体:

database