Search code examples
jsonflutterdarthttp-post

Flutter/ Dart: Add items from list to JSON body


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.


Solution

  • 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?