In this example, you will learn how to remove single or multiple items from a Map using a key or value in Dart or Flutter. You may need to remove the specific element from the Map under different conditions. See the example below to remove an item from a Map.
Map values = {
"a":"Elephant",
"b":"Horse",
"c":"Lion",
"d":"Deer"
};
values.remove("c");
print(values);
//Output: {a: Elephant, b: Horse, d: Deer}
Here, the item with key "c" will be removed from Map.
Map values = {
"a":"Elephant",
"b":"Horse",
"c":"Lion",
"d":"Deer"
};
values.removeWhere((key, value) => value == "Lion");
print(values);
//Output: {a: Elephant, b: Horse, d: Deer}
Here, the item with the value "Lion" will be removed.
Map values = {
"a":"Elephant",
"b":"Horse",
"c":"Lion",
"d":"Deer"
};
values.removeWhere((key, value){
return value == "Lion" || value == "Deer";
});
print(values);
//output: {a: Elephant, b: Horse}
Here, the Item with the value "Lion" and "Deer" is removed. You can apply your own logical condition by using both key and value to remove the item.
In this way, you can remove items from Map in Dart or Flutter.
Please Wait...
No any Comments on this Article