Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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

A List is one type of Iterable, but not all Iterables are Lists. Does your _DropDownList function actually need to take a List argument, or could it take an Iterable instead? If so, I'd just change _DropDownList's parameter type to be more general. Otherwise you'll need to create a List from the Iterable by calling .toList() on it. – jamesdlin Mar 3, 2022 at 12:00

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') });
var mappedValues = mappedList.map((m) => { 'headsign': m.getString('headsign'), 'time': m.getString('time') }).toList();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.