Home > Blockchain >  remove item from a map of string
remove item from a map of string

Time:10-09

I have a map of string Map<String, String> data = {}; and I added user input items and store it in the map. but when I try to remove the item the key/id changes and the value becomes null.

this is what I have when I added the items and remove them after.

//called on onChanged
if (data.containsKey('$index') && value != null) {
   data['$index'] = value!;
}

//called on remove
data.remove('$index');
data.removeWhere((key, value) => key == "");

//added result
print(${json.encode(data)});

before remove called=> {"0":"Manager","1":"Service","2":"Sales","3":"Reception"},

after remove called=> {"0":"Manager","1":"Service","3":"Reception","4":""}

what should be => {"0":"Manager","1":"Service","2":"Sales"}

so what is the problem here?

CodePudding user response:

Use .remove(key) to remove all key along with its value.

  final data = {
    "0": "Manager",
    "1": "Service",
    "2": "Sales",
    "3": "Reception"
  };
  data.remove("3");
  print(data); //{0: Manager, 1: Service, 2: Sales}
});

More about /Map/remove

  • Related