Home > database >  Flutter/ Dart: Add items from list to JSON body
Flutter/ Dart: Add items from list to JSON body

Time:02-19

I have a situation: I need to push images to Airtable using their Rest API (from my flutter app) like this:

POST https://api.airtable.com/v0/<base>/<table name> \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "fields": {
    "Pictures": [
      {
        "url": "imageUrl1"
      },
      {
        "url": "imageUrl2"
      }
    ],
    "Variation": "Original"
  }
}'

I have a list of urls which is generated after uploading images to cloud storage and it looks like this:

imageURL = [imageUrl1, imageUrl2, imageUrl3, ... ]

I need help inserting the urls from list to json body.

I was able to do this by converting the list to map like: {url: imageUrl}, but the output always gives me a map with length 1.

I am sure there's a simple solution to this, but I am unable to figure it out. Any help will be highly appreciated.

Thanks in advance.

CodePudding user response:

if you have something like:

var imageURL = ['url1', 'url2', 'url3', 'url4'];

and you do something like:

 var mapped = imageURL.map((url) => { 'url': url }).toList();

Then the output would look like:

[{url: url1}, {url: url2}, {url: url3}, {url: url4}]

Is that what you're looking for to do?

CodePudding user response:

try this get the list and do .map

//Body to pass

final data = {
     "fields": {
        "Pictures": imageURL.map((e)=> 
           {
             // e is the data which the imageUrl1 and so fort
             "url": e
           }
             ).toList(),
         "Variation": "Original"
    }

};
  • Related