Post-Array mit http im Flattern

Oct 12 2020

Weiß jemand, wie man ein Array mit postet http?

var headers = {"Accept": "application/json"};
      var url = "https://...."
      var response = await http.post(url,
          body: {
           ....
          },

Ich habe einen Parameter namens commentList. Ich muss passieren comment[0], comment[1]... im Körper.

Wie for loopim Körper verwenden?

for(var i in list){
"comments"+"["+index+"]" = i;
   index ++; 
}

Hier der Postbotenschlüssel und Wert

Antworten

BilalŞimşek Oct 12 2020 at 02:02

Wenn Sie Kommentare aus einem bestimmten Grund kombinieren müssen, können Sie dies tun.

List commentList = ['comment1','comment2','comment3'];
Map<String, dynamic> body;

kombinieren Sie sie:

String comments ='';
  
  commentList.forEach((comment){
    
    comments +=comment+' ';
    
  });
 body = {'combinedComments':comments};

dann poste:

var headers = {"Accept": "application/json"};
      var url = "https://...."
      var response = await http.post(url,
          body: this.body);

Oder verwenden Sie einfach json encode, um Ihre Liste in einen String zu konvertieren.

Map<String,dynamic> body = {'comments' :jsonEncode(commentList)};

und benutze dies als Körper zum Posten.

Tony Oct 12 2020 at 14:25

Ich habe es geschafft, es zu beheben.

int count = 0;

var url = "https://xxx";
      var response = await http.post(url,
          body: {
            for (var i in list) 'comments[${count++}]':i,
            ....
          },
          headers: headers);