0

I have a http request like this using the http package:

import 'package:http/http.dart' as http;

Uri url = Uri.http(
  'http://sample.com',
  '/request',
  {
    'dataMap': {
      'key1': ['item1', 'item2']
      'key2': ['item1', 'item2']
    },
  },
);

I get this error:

 _TypeError (type '_InternalLinkedHashMap<String, List<String>>' is not a subtype of type 'Iterable<dynamic>')

The error is not thrown if I don't put a map in the query parameters.

So how do I send a Map?

Nathan Tew
  • 432
  • 1
  • 5
  • 21
  • can you please provide some more info. About methods and is the items in key list are fix or what. It would be good if you provide info. – Sagar R Anghan Feb 28 '22 at 07:18

2 Answers2

1

You could jsonEncode the map:

import 'dart:convert';

void main() {
  Uri url = Uri.http(
    'sample.com',
    '/request',
    {
      'dataMap': jsonEncode({
        'key1': ['item1', 'item2'],
        'key2': ['item1', 'item2']
      }),
    },
  );
  
  print(url);
}
mmcdon20
  • 5,767
  • 1
  • 18
  • 26
  • thanks, this worked. For anyone who might have the same problems as me, use `json.loads` to "decode" the map in python if you're using a flask backend: https://stackoverflow.com/questions/7771011/how-to-parse-data-in-json-format – Nathan Tew Feb 28 '22 at 07:23
1

You can achieve this way :

  _request() async {
    
    
    var outer = {};
    
    outer['dataMap'] =  {
          'key1': ['item1', 'item2'],
           'key2': ['item1', 'item2']
    };
    
    print(jsonEncode(outer));
 
    
    
    return  http.post(Uri.parse('https://www.example.com/request'), body: jsonEncode(outer));
    
    
  }
Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14