I have a map based on conversation ids:

Map<String, List<Message>> user_id_mapped_to_messages_list = new Map();

I add data like this in the loading part of the app:

 user_id_mapped_to_messages_list[conversation_id].add(message_to_current_user);
 user_id_mapped_to_messages_list[conversation_id_reversed].add(message_to_current_user);

The next phase is loading them into a message chat. I'm trying to show only messages with the correct conversation id's into the chat ListView.

How can I accomplish this and what is the proper way to load a map with a list, filter it, and display it on the ListView as data?

                 child: ListView.builder(
                    reverse: true,
                    padding: EdgeInsets.only(
                      top: 15.0,
                    ),
                    itemCount: user_id_mapped_to_messages_list["${widget.user.id}-${current_user.id}"].length + user_id_mapped_to_messages_list["${widget.user.id}-${current_user.id}"].length ,
                    itemBuilder: (BuildContext context, int index) {
//                      Message message = messages[index];
//                      bool isMe = message.sender.id == current_user.id;
                      Message message;
                      bool isMe;
                      String conversation_id = "${widget.user.id}-${current_user.id}";
                      String conversation_id_reversed = "${widget.user.id}-${current_user.id}";

                      if(user_id_mapped_to_messages_list[conversation_id] != null || user_id_mapped_to_messages_list[conversation_id_reversed] != null){
                        print("Adding message == ========= =========== ---------------");

                        if(user_id_mapped_to_messages_list[conversation_id][index] !=null) {
                          print("Adding message user_id_mapped_to_messages_list[conversation_id][index] !=null");
                          message = user_id_mapped_to_messages_list[conversation_id][index];
                          isMe = message.sender.id == current_user.id;
                          return _buildMessage(message, isMe);
                        }else{
                          if( user_id_mapped_to_messages_list[conversation_id_reversed][index] == null) {
                            //message =  user_id_mapped_to_messages_list[conversation_id_reversed][0]; //idk
                            isMe = message.sender.id == current_user.id;
                          }else {
                            print("Adding message else from me");
                            message = user_id_mapped_to_messages_list[conversation_id_reversed][index];
                            isMe = message.sender.id == current_user.id;
                          }
                          return _buildMessage(message, isMe);
                        }
                      }else{

                       //not sure how to tell the list to skip null
                       return null;
                      }

                    },
                  ),


Solution 1: Petro

Ok so I was thinking about this wrong, and simplified the solution. All I needed to do was set the id to only be the chat id of the user I was talking to, I didn't need to split up my user id from the other user's id. So I scrapped conversation ids and went with this simplified version:

Loading code had logic like this:


Message to me:

   print("Message is going to the logged-in user id of: ${current_user.id}");
   user_id_mapped_to_messages_list[from_user_id].add(message_to_current_user);

Or message sent by me:

   print("Message is coming from us and going to: ${to_user_id}");
   user_id_mapped_to_messages_list[to_user_id].add(message_from_current_user);

ListView Builder was passed the map of the "other user" In our conversation, which now contains all of the messages

ListView Code:

     itemCount: user_id_mapped_to_messages_list["${widget.user.id}"].length ,
            itemBuilder: (BuildContext context, int index) {
            Message message = user_id_mapped_to_messages_list["${widget.user.id}"][index];
            bool isMe = message.sender.id == current_user.id;
            return _buildMessage(message, isMe);