Home > Software engineering >  how to check if a typing value contains a list of map
how to check if a typing value contains a list of map

Time:03-06

I have a list of map, so I want to check If the value I type contains any of the key value before adding.

here is my code:

for (var check in disciplineList) {
                        if (check.containsKey('degree') ||
                            !check.containsKey('degree')) {
                          if (check['degree'] != discipline.text) {
                            disciplineList.add({
                              'degree': discipline.text,
                              'date': currentDate
                            });
                            setState1(() {});
                            setState(() {});
                            discipline.clear();
                            currentDate = DateTime.now();
                            print(disciplineList);
                          } else {
                            openCustomDialog(
                                context: context,
                                body: '',
                                heading: 'Item Already Exists!');
                            print("Item Already Exists!");
                          }
                        }
                      }

CodePudding user response:

In case you have some List of Maps listOfMaps and you want to check if it contains

1- a specific key

you can do so like this:

bool doesItContainKey(var key)
{
   return listOfMaps.any((element) => element.keys.contains(key)); 
}

2- a specific value

you can do so like this:

bool doesItContainValue(var value)
{
   return listOfMaps.any((element) => element.values.contains(value)); 
}

3- a specific map:

you can do so like this:

bool doesItContainMap(var map)
{
   return listOfMaps.any((element) => element==map); 
}
  • Related