How to insert in ListView without rebuild in Flutter

Viewed 418

I'm creating a chat, and the problem is that, for example, when there is a video or voice message in the messages, then when insert completely rebuilds everything and fails to watch or listen to the message if the person writes. How to make sure that not everything is rebuilt.

I read that if the key is the same, then this element will not be rebuilt, but I still have it rebuilt. As a key, I give the time of the message.

PagewiseListView<MessageData>(
      reverse: true,
      shrinkWrap: true,
      cacheExtent: 6500,
      controller: _scrollController,
      physics: BouncingScrollPhysics(),
      padding: EdgeInsets.only(top:10.0, left: 8, right: 8,bottom: getHeight(70)),
      loadingBuilder: (c) =>  LoaderPage()
      addAutomaticKeepAlives: true,
      itemBuilder: (c,message,index) => UserMessage(data: message, index: index, toUserID: widget.toUserid, player: mPlayer, key: message.key),
      showRetry: true,
      pageLoadController: pageLoadControllerMessage,
    ),
1 Answers

I have had this issue as well.

Something that you can do is to assign everything keys. All the widgets that have dynamic values and their parents, etc. Its a little tedious, but definitely worth it.

Another thing that you can do is to break up your widget by extracting parts of your code as individual widgets. When you create new widgets, Flutter will only rebuild the widgets that need rebuilding.

If you continue to have this issue, you can also wrap your widgets in Builder Widgets. this only works if you are not using setState() to update your state. Builder will contain your rebuilds to that specific section of code.

Related