duytq94 / flutter-chat-demo

The chat app made by Flutter and Firebase
MIT License
1.74k stars 676 forks source link

Snapshot _limit variable needs a constraint to prevent from indefinite increment #58

Closed KartikShrivastava closed 2 years ago

KartikShrivastava commented 2 years ago

Hi Duy,

A quick issue, somewhat related to previous one but different scenario happening due to following lines: https://github.com/duytq94/flutter-chat-demo/blob/f58e1b421d72f2c17fdb5fc0a9d1f7bfde3021dd/lib/pages/chat_page.dart#L74

What I noted is if user tries to scroll to the top of chat messages the _limit variable which fethes snapshots has no criteria to stop incrementing. In case user tries to scroll to top quite few times then _limit value becomes unrealistically large and defeats the purpose of loading limited number of snapshots.

I'm proposing few changes in _scrollListener() which I'm using to limit the _limit:

  _scrollListener() {
    if (messageListScrollController.offset >= messageListScrollController.position.maxScrollExtent &&
        !messageListScrollController.position.outOfRange) {
      ///   Stop calling set state when no more messages are there to load
      if (_limit - numMessagesLoaded < 30)
        setState(() => _limit += _limitIncrement);
      else
        print('No more messages to load');
    }
  }

Additional changes include:

Thank you, Kartik

duytq94 commented 2 years ago

Hi, you're right, it will increase infinitely I just fix it at https://github.com/duytq94/flutter-chat-demo/commit/c6ef5ba16ef02392b8bb2dd4f775bb4eb32858a2