skip to Main Content

i’m try to make Search Bar and Searching from Map<String, List>
but i got error like this –> "A value of type ‘Iterable<MapEntry<String, List>>’
can’t be assigned to a variable of type ‘Map<String, List>"

//here the function that i try.
Map<String, List> datasource_map = {};

Map<String, List> result = {};
void updateList(String enteredKeyword) {

setState(() {
  result = datasource_map.entries.map((e) {
    return MapEntry(
        e.key,
        e.value
            .where(
              (element) => element.foodName!.toLowerCase().contains(
                    enteredKeyword.toLowerCase(),
                  ),
            )
            .toList());
  });
  print("### $result");
});

}

this is my Model of foodCard

what should i try ? i am new at flutter

2

Answers


  1. Chosen as BEST ANSWER

    Thank you everyone! I solved it.

    This works:

    Map<String, List<dynamic>> result = {};
    
      void updateList(String enteredKeyword) {
        Map<String, List<dynamic>> mapResult = {};
    
        datasource_map.forEach((key, value) {
          List foodByFilter = value.where((element) {
            return element.foodName
                .toLowerCase()
                .contains(_textEditingController.text.toLowerCase().toString());
          }).toList();
    
          if (foodByFilter.isNotEmpty) mapResult[key] = foodByFilter;
        });
    
        setState(() {
          result = mapResult;
        });
      }
    

  2. Looks like your datasource_map.entries is a List – calling a map() on it will produce an Iterator – not a Map object.

    You should use Map.fromEntries constructor:

    
    result = Map.fromEntries(datasource_map.entries.map((e) {
        return MapEntry(
            e.key,
            e.value
                .where(
                  (element) => element.foodName!.toLowerCase().contains(
                        enteredKeyword.toLowerCase(),
                      ),
                )
                .toList());
      }));
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search