Reputation: 2657
Aye aye good people,
I need to be able to add and delete elements from a list stored inside a map;
I wrongly assumed that this:
final Map<int,List<int>> _map = Map();
_map[1].add(1);
int _data=_map[1][0];
or this:
Map<int,List<int>> _map = Map<int,List<int>>();
List _list = List();
_list.add(1);
_map[1]=_list;
int _data=_map[1][0];
would work, but doesn't.
So... how does it work?
Thank you in advance,
Francesco
Upvotes: 0
Views: 2498
Reputation: 76183
The first _map[1]
tries to get the value in the map _map
for the given key 1
. But at this time _map
is empty so it returns null
.
To lazy initialize your map, you can do:
final Map<int,List<int>> _map = Map();
_map.putIfAbsent(1, () => <int>[]).add(1);
int _data=_map[1][0];
Upvotes: 2