Hey there, I am trying to initialise a map
Map<String, List<String> > mp with a value, I am attaching a code snippet below. However, it's giving me an error that the add() method was called on null object. Moreover, when I tried fixing it by using method 2, it gave me the error mentioned below it.
Approach 1:values.forEach((item){
List<String> prop = item.split('/');
mp[prop[0]].add(prop[1]);
}
);
The above approach is giving me the null error so I tried this other approach.
Approach 2:
values.forEach((item){
List<String> prop = item.split('/');
final key = prop[0];
mp[key] = (mp[key] == null) ? List<String>():mp[key];
mp[key].add(prop[1]);
}
);
This approach gave me an error Unhandled Exception: RangeError (index): Invalid value: Only valid value is 0: 1
How do I resolve this error?