There is a Dismissible widget that has a function to 'undo' the item map deletion. When I Dismiss an item that is not the last from you key on the map, it works fine, but when the item is the last item of your key it throws this error:
The method 'add' was called on null. Receiver: null Tried calling: add("Test")
This is the code:
return Dismissible(
key: Key(event),
child: ....my child
onDismissed: (direction) {
setState(() {
_events[_thisDay].remove(event);
if(_events[_thisDay].length == 0){
_events.remove(_day);
}
});
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(
"Event $event dismissed !"),
action: SnackBarAction(
label: "Undo",
onPressed: () => setState(() =>_events[_thisDay].add(event))), // HERE
));
},
background: Container(color: Colors.red),
);
*How can I treat this error? _events is null. _thisDay isn't
nullcheck (_events[_thisDay]?.add(event)) before addingeventto your list. Then, you have to check why is_events[_thisDay]null? - Ravi Singh Lodhi_events? - Ravi Singh Lodhi