Skip to content

Commit 5e6599e

Browse files
authored
fix: 🐛 ScrollController is disposed when parent widget state changes (#289) (#333)
1 parent 7e1d854 commit 5e6599e

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [2.4.2] (unreleased)
2+
3+
* **Fix**: [289](https://github.com/SimformSolutionsPvtLtd/flutter_chatview/issues/289)
4+
ScrollController is disposed when parent widget state changes
5+
16
## [2.4.1]
27

38
* **Important**: Updated minimum SDK version to `3.4.0`

example/lib/main.dart

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,50 @@ class ChatScreen extends StatefulWidget {
3535
class _ChatScreenState extends State<ChatScreen> {
3636
AppTheme theme = LightTheme();
3737
bool isDarkTheme = false;
38-
final _chatController = ChatController(
39-
initialMessageList: Data.messageList,
40-
scrollController: ScrollController(),
41-
currentUser: const ChatUser(
42-
id: '1',
43-
name: 'Flutter',
44-
profilePhoto: Data.profileImage,
45-
),
46-
otherUsers: const [
47-
ChatUser(
48-
id: '2',
49-
name: 'Simform',
50-
profilePhoto: Data.profileImage,
51-
),
52-
ChatUser(
53-
id: '3',
54-
name: 'Jhon',
55-
profilePhoto: Data.profileImage,
56-
),
57-
ChatUser(
58-
id: '4',
59-
name: 'Mike',
60-
profilePhoto: Data.profileImage,
61-
),
62-
ChatUser(
63-
id: '5',
64-
name: 'Rich',
38+
late final ChatController _chatController;
39+
40+
@override
41+
void initState() {
42+
super.initState();
43+
_chatController = ChatController(
44+
initialMessageList: Data.messageList,
45+
scrollController: ScrollController(),
46+
currentUser: ChatUser(
47+
id: '1',
48+
name: 'Flutter',
6549
profilePhoto: Data.profileImage,
6650
),
67-
],
68-
);
51+
otherUsers: [
52+
ChatUser(
53+
id: '2',
54+
name: 'Simform',
55+
profilePhoto: Data.profileImage,
56+
),
57+
ChatUser(
58+
id: '3',
59+
name: 'Jhon',
60+
profilePhoto: Data.profileImage,
61+
),
62+
ChatUser(
63+
id: '4',
64+
name: 'Mike',
65+
profilePhoto: Data.profileImage,
66+
),
67+
ChatUser(
68+
id: '5',
69+
name: 'Rich',
70+
profilePhoto: Data.profileImage,
71+
),
72+
],
73+
);
74+
}
75+
76+
@override
77+
void dispose() {
78+
// ChatController should be disposed to avoid memory leaks
79+
_chatController.dispose();
80+
super.dispose();
81+
}
6982

7083
void _showHideTypingIndicator() {
7184
_chatController.setTypingIndicator = !_chatController.showTypingIndicator;

lib/src/widgets/chat_list_widget.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ class _ChatListWidgetState extends State<ChatListWidget> {
245245

246246
@override
247247
void dispose() {
248-
chatController.messageStreamController.close();
249-
scrollController.dispose();
250248
_isNextPageLoading.dispose();
251249
super.dispose();
252250
}

0 commit comments

Comments
 (0)