diff --git a/CHANGELOG.md b/CHANGELOG.md index cf68460c..7a3f45b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,8 @@ send message not working when user start texting after newLine. * **Fix**: [191](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/191) Fix error when using `BuildContext` or `State` extensions when not mounted. +* **Fix**: [192](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/pull/192) Fix + send to closed socket or animate on `ScrollController` without clients. ## [1.3.1] diff --git a/lib/src/controller/chat_controller.dart b/lib/src/controller/chat_controller.dart index b1649911..2a5b148f 100644 --- a/lib/src/controller/chat_controller.dart +++ b/lib/src/controller/chat_controller.dart @@ -89,7 +89,9 @@ class ChatController { /// Used to add message in message list. void addMessage(Message message) { initialMessageList.add(message); - messageStreamController.sink.add(initialMessageList); + if (!messageStreamController.isClosed) { + messageStreamController.sink.add(initialMessageList); + } } /// Used to add reply suggestions. @@ -134,24 +136,31 @@ class ChatController { messageType: message.messageType, status: message.status, ); - messageStreamController.sink.add(initialMessageList); + if (!messageStreamController.isClosed) { + messageStreamController.sink.add(initialMessageList); + } } /// Function to scroll to last messages in chat view void scrollToLastMessage() => Timer( const Duration(milliseconds: 300), - () => scrollController.animateTo( - scrollController.position.minScrollExtent, - curve: Curves.easeIn, - duration: const Duration(milliseconds: 300), - ), + () { + if (!scrollController.hasClients) return; + scrollController.animateTo( + scrollController.position.minScrollExtent, + curve: Curves.easeIn, + duration: const Duration(milliseconds: 300), + ); + }, ); /// Function for loading data while pagination. void loadMoreData(List messageList) { /// Here, we have passed 0 index as we need to add data before first data initialMessageList.insertAll(0, messageList); - messageStreamController.sink.add(initialMessageList); + if (!messageStreamController.isClosed) { + messageStreamController.sink.add(initialMessageList); + } } /// Function for getting ChatUser object from user id