Error while assigning value to a map

33 views
Skip to first unread message

Vaibhav Kushwaha

unread,
Aug 29, 2020, 7:03:21 AM8/29/20
to Flutter Development (flutter-dev)
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?

Suzuki Tomohiro

unread,
Aug 29, 2020, 7:24:12 AM8/29/20
to Vaibhav Kushwaha, Flutter Development (flutter-dev)
1. Learn how to use debugger. Add breakpoint at the problematic line to see why these exceptions occur.

2. Would you share some example for “values” variable?

--


You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.


To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/4c27accf-e481-46e7-9c70-acb8fa1edd02n%40googlegroups.com.


Reply all
Reply to author
Forward
0 new messages