Home > Software engineering >  The argument type 'Iterable<Map<String, dynamic>>' can't be assigned to t
The argument type 'Iterable<Map<String, dynamic>>' can't be assigned to t

Time:03-04

I have the below function to take List data from API call and populate pop-up metadata with the first 5 instances of key/value pairs.

   var mappedList = boards.boards!.take(5).toList(); 


   // now, from the list above, I map each result into a key-value pair

    var mappedValues = mappedList.map((m) => { 'headsign': m.getString('headsign'), 'time': m.getString('time') });

   // this list will look like:
   // [{ headsign: 'value1', title: 'title1'}, { headsign: 'value2', title: 'value2' }]


   // Now moving mappedValues list to showDialog
   String title = ("Title Test");

   _DropDownList(mappedValues, title);
   });
  }


  Future<void> _DropDownList(List<Map<String, dynamic>> values, String title) async {
         var test1 = ListBody(
              ...

I receive the error

    The argument type 'Iterable<Map<String, dynamic>>' can't be assigned to the parameter type 'List<Map<String, dynamic>>'

on the line

     _DropDownList(mappedValues, title); //(on 'mappedValues')

This error links to

   var mappedValues = mappedList.map((m) =>

And the error is with the map section of the function. I have tried casting as etc but can't get it to work or go through as a List<Map<String, dynamic>>

Thanks

Update

If I change to iterable like so I get the error The operator '[]' isn't defined for the type 'Iterable<Map<String, dynamic>>'. on [index] where highlighted below

Future<void> _DropDownList(Iterable<Map<String, dynamic>> values, String title) async {
         var test1 = ListBody(
              children: List.generate(
                values.length, (index) {
   *[index]*                return Text(values[index]['headsign']);
                }
              ),
            );

Thanks

CodePudding user response:

You can easily change an Iterable to a list by calling toList() so just change

var mappedValues = mappedList.map((m) => { 'headsign': m.getString('headsign'), 'time': m.getString('time') });

to

var mappedValues = mappedList.map((m) => { 'headsign': m.getString('headsign'), 'time': m.getString('time') }).toList();

CodePudding user response:

Just try to add .toList for the map type list. Something like this -

var mappedValues = mappedList.map((m) => { 'headsign': m.getString('headsign'), 'time': m.getString('time') }).toList();
  • Related