Pubblica array utilizzando http in flutter
Oct 12 2020
Qualcuno sa come inviare array utilizzando http?
var headers = {"Accept": "application/json"};
var url = "https://...."
var response = await http.post(url,
body: {
....
},
Ho un param di nome commentList. Ho bisogno di passare comment[0], comment[1]... dentro il corpo.
Come si usa for loopall'interno del corpo ???
for(var i in list){
"comments"+"["+index+"]" = i;
index ++;
}
Qui la chiave e il valore del postino
Risposte
BilalŞimşek Oct 12 2020 at 02:02
se hai bisogno di combinare commenti per un motivo specifico, puoi farlo.
List commentList = ['comment1','comment2','comment3'];
Map<String, dynamic> body;
combinali:
String comments ='';
commentList.forEach((comment){
comments +=comment+' ';
});
body = {'combinedComments':comments};
quindi posta:
var headers = {"Accept": "application/json"};
var url = "https://...."
var response = await http.post(url,
body: this.body);
o usa semplicemente la codifica json per convertire la tua lista in stringa;
Map<String,dynamic> body = {'comments' :jsonEncode(commentList)};
e usalo come corpo per il post.
Tony Oct 12 2020 at 14:25
Sono riuscito a risolverlo.
int count = 0;
var url = "https://xxx";
var response = await http.post(url,
body: {
for (var i in list) 'comments[${count++}]':i,
....
},
headers: headers);