-1

Anyone know how to post array using http?

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

I have a param named commentList. I need to pass comment[0], comment[1]... inside body.

How to use for loop inside the body???

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

Here the postman key and value

enter image description here

6
  • What about JSONing that array? Commented Oct 11, 2020 at 17:14
  • @raina77ow any example? Commented Oct 11, 2020 at 17:15
  • stackoverflow.com/questions/53002196/… Commented Oct 11, 2020 at 17:17
  • @raina77ow I would like to post it as comment[0],comment[1]. Is the link provided work in my case? Commented Oct 11, 2020 at 17:21
  • just pass the whole array in jsonEncode @Tony Commented Oct 11, 2020 at 17:57

2 Answers 2

0

if you need to combine comments for specific reason you can do like this.

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

combine them:

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

then post :

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

or just use json encode to convert your list to string;

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

and use this as body to post.

Sign up to request clarification or add additional context in comments.

Comments

0

I managed to fix it.

int count = 0;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.