Search code examples
flutterdartdata-structuresreset

How to (re)set all values of a map in Dart/Flutter


I have a simple map:

Map<String, bool> selection = {};

Then the entries are added. At some point, I want to reset the selection, i.e.:

for (var name in selection.keys) {
   selection[name] = false;
}

Is there a way to reset all values to false, using only one statement in Dart?

For example, the following statement is very convenient and concise to check if the selection is not empty:

if( selection.values.contains(true) )

So, I'm looking for something similar to reset all the values to false.

Rk: Out of scope answers ;-) :

  • forEach
  • writing a method/function to do the stuff

Addendum

Just found and tested this one:

selection.updateAll((name, value) => value = false);

But there could be something even more simple, i.e. without a lambda function.


Solution

  • selection.forEach((name, value) => selection[name] = false);

    I think this is what you are looking for. A simple one-liner solution.

    Edit

    selection.updateAll((name, value) => value = false);

    is also an option, as you have added

    Apart from those, there is no predefined method that allows you to achieve what you have described.