flyerhq / flutter_chat_ui

Actively maintained, community-driven chat UI implementation with an optional Firebase BaaS.
https://flyer.chat
Apache License 2.0
1.52k stars 663 forks source link

messageList.insert(0, message) always shows incoming messages as my own. #595

Open UmairSaqibBhutta opened 1 month ago

UmairSaqibBhutta commented 1 month ago

I am fetching other user messages from Firebase and showing them in the chat list. but it always indicates streambuilder data messages as my own. I am given the proper author ID but I have the same issue.

 StreamBuilder<QuerySnapshot>(
                  stream: firestore.collection(widget.roomId).snapshots(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      if (snapshot.data != null &&
                          snapshot.data!.docs.isNotEmpty) {
                        log("snapshot firebase = ${snapshot.data!.docs[0].data()}");
                        var message = Message.fromJson(snapshot.data!.docs[0]
                            .data() as Map<String, dynamic>);
                        log("message of firestore = ${message.createdAt}");
                        log("message.author.id = ${message.author.id}");
                        log("userId = ${userId}");
                        if (message.author.id != userId) {
                          log("adding msg");
                          if (messageList[0].id != message.id) {
                            messageList.insert(0, message);
                          }

                        }
                      }
                    }
                    return Chat(
                      theme: DefaultChatTheme(
                        // backgroundColor: Colors.grey[300] as Color,
                        backgroundColor: Colors.transparent,
                      ),

                      isAttachmentUploading: isAttachmentUploading,
                      onMessageLongPress: (message) {
                        log("onMessageLongPress fired");
                        messageLongPressOption(message);
                        if (message is types.TextMessage) {
                          log("message text = ${message.text}");
                        }
                      },

                      emptyState: Container(
                          alignment: Alignment.center,
                          margin: const EdgeInsets.symmetric(
                            horizontal: 24,
                          ),
                          child: isEmptyState == true
                              ? Text("No Message yet!",
                                  style:
                                      ralewayFont(fontWeight: FontWeight.bold))
                              : CupertinoActivityIndicator(
                                  color: Colors.indigo,
                                  radius: 20,
                                  animating: true,
                                )),
                      // Image.asset("assets/loading.gif")),
                      messages: messageList,
                      onAttachmentPressed: _handleAtachmentPressed,
                      onMessageTap: _handleMessageTap,
                      // onPreviewDataFetched: _handlePreviewDataFetched,
                      onSendPressed: _handleSendPressed,
                      onSendRecordingFilePressed: _handleSendRecordingPressed,
                      onReplyMessagePressed: (e) {
                        log("onReplyMessagePressed is fired in chat page");
                      },
                      user: _user,
                      onAvatarTap: (user) {
                        showToast(user.firstName.toString());
                      },
                      showUserNames: true,
                      showUserAvatars: true,
                      // groupMessagesThreshold: 6000,
                      usePreviewData: true,
                      onMessageStatusTap: (e) {
                        showToast("sended message");
                      },
                    );
                  }),
demchenkoalex commented 1 month ago

you mean that all messages are showed on 1 side?

UmairSaqibBhutta commented 1 month ago

yes exactly.

demchenkoalex commented 1 month ago

alignment is set here https://github.com/flyerhq/flutter_chat_ui/blob/2b5978c33600ec8d3574e6828c6cd030cafc254a/lib/src/widgets/message/message.dart#L357 where final currentUserIsAuthor = user.id == message.author.id;. I see you pass _user so make sure it's id equals to id of author.id field of a message when you want messages aligned to the right, and not equals if you want messages on the left.

in your logic I also see you do

if (message.author.id != userId) {
  log("adding msg");
  if (messageList[0].id != message.id) {
    messageList.insert(0, message);
  }
}

and then you use messageList as message and not snapshot data, so according to your logic you only add messages where message author is not current user, so you will only see messages on the left side