Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
UmairSaqibBhutta opened this issue May 13, 2024 · 3 comments
Labels
question Further information is requested

Comments

@UmairSaqibBhutta
Copy link

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");
                      },
                    );
                  }),

@UmairSaqibBhutta UmairSaqibBhutta added the question Further information is requested label May 13, 2024
@demchenkoalex
Copy link
Member

you mean that all messages are showed on 1 side?

@UmairSaqibBhutta
Copy link
Author

yes exactly.

@demchenkoalex
Copy link
Member

alignment is set here

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants