Looking for an example in dart exactly like this question: How to add element into ArrayList in HashMap
Map<int, List<Message>> user_id_mapped_to_messages_list = new HashMap();
This throws a null insert, what am I missing here?
user_id_mapped_to_messages_list[user_id].add(message_to_current_user);
Error:
I/flutter (11992): ==============================================
I/flutter (11992): Message is going to the logged in user id of: 11
I/flutter (11992): Message details:
I/flutter (11992): sender: Instance of 'User'
I/flutter (11992): time: 2020-05-11 22:28:26
I/flutter (11992): text: Message 2 from John to Joe
I/flutter (11992): message added to chat group
E/flutter (11992): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'add' was called on null.
E/flutter (11992): Receiver: null
E/flutter (11992): Tried calling: add(Instance of 'Message')
E/flutter (11992): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (11992): #1 _myappnameMessagesRecentChatsState.getConversations.<anonymous closure>.<anonymous closure>
(package:myappname_app_androidx/widgets/home/messages/messages_v2/recent_chats.dart:158:83)
No items existed... for loop printing the messages in the array list
user_id_mapped_to_messages_list.forEach((k,v) => print('${k}: ${v}'));
user_id_mapped_to_messages_list[user_id].add(message);