Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading