Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an array in dart post request

I try to send messages to device groups with their registration_ids.

This my code:

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

When the app runs this error displays:

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

How to fix it?

like image 796
karrar jasim Avatar asked Sep 21 '25 05:09

karrar jasim


1 Answers

Problem solved; I just encoded the body:

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"
 }
)
}
like image 174
karrar jasim Avatar answered Sep 22 '25 20:09

karrar jasim