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

MessagesListController not able to clear items when enter chatPage again from another chatid #21

Closed
luohao123 opened this issue Apr 30, 2021 · 6 comments

Comments

@luohao123
Copy link

Here is my code:

gLogger.i("init controller items: ${_controller.items.length}");
    _controller.items.clear();
    _controller.notifyChanges();
    _chatBloc.fetchLocalMsgsForSession(_chat.id);
    subscription = _chatBloc.crtChatMsgs.listen((event) {
      _controller.addAll(event);
      _controller.notifyChanges();
      gLogger.i("now controller items: ${_controller.items.length}");
    });

this is inside my initState function, so I want clear items every time enter a new chatPage. But it doesn't do this.

Normally, first log should be 0, and every time crtChatMsgs only fetechs 25 msgs from local. So everytime it enters chatPage, it should be 25 msgs.

However, first time:

image

this is normal, then I switch to another chatid (another chat page):

image

As u can see, second time the msg doesn't clear at all. It becomes 50, double of them. Normally, it should be fresh 25 msgs.

And the UI actually doesn't updated still contains data from previous chatid.

What could be the reason?

(I am new to flutter maybe I did something wrong but this lib is really useful, so I use it)

@luohao123 luohao123 changed the title MessagesListController not able to clear items when inter chagePage again from another chatid MessagesListController not able to clear items when enter chagePage again from another chatid Apr 30, 2021
@luohao123 luohao123 changed the title MessagesListController not able to clear items when enter chagePage again from another chatid MessagesListController not able to clear items when enter chatPage again from another chatid Apr 30, 2021
@jonasN5
Copy link
Owner

jonasN5 commented Apr 30, 2021

First off, I'm not sure I understand your flow. Usually to leave a ChatScreen, the user will go BACK and therefore pop the destination from the stack to return to ChatsScreen (the list of Chats), therefore also disposing the controller.

When you call _controller.items.clear();, it has nothing to do with the library. You're simply calling clear() on a dart list. The fact that you have 50 items instead of 25 after that indicates that you have a problem somewhere else, since clear() cannot fail. I suspect that it has something to do with the way you handle your controller. When accessing a new ChatScreen, you should create a new controller instance (which resets the items).

@luohao123
Copy link
Author

luohao123 commented Apr 30, 2021

vNot work even I add a clear at dispose!!!!!!
this is really bug!!

image

@luohao123
Copy link
Author

@themadmrj I am currently found, even I re-enter same chatid page, the msg still increase, rather than first clear and then add only 25 msgs.

And my observation is that seems the stream call twice..... But I don't know why it called twice... I am gonna to cry about this.

Here is my code of navigate to detail:

final cp =
        ChatScreen(ChatScreenArgs(chat: chat, chatBloc: widget.chatBloc));
    var res = await Navigator.of(context)
        .push(CupertinoPageRoute(builder: (context) => cp));

@jonasN5
Copy link
Owner

jonasN5 commented Apr 30, 2021

Good old debugging process, just add print everywhere and eventually you'll find the problem!
Closing since solved/not related to library.

@jonasN5 jonasN5 closed this as completed Apr 30, 2021
@luohao123
Copy link
Author

I solved my problem, thanks!

@luohao123
Copy link
Author

For future people might got this issue... I dont' plan to tell u guys..

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

No branches or pull requests

2 participants