@@ -545,38 +545,42 @@ class StreamMessageInputState extends State<StreamMessageInput>
545545 _effectiveFocusNode.addListener (_focusNodeListener);
546546
547547 WidgetsBinding .instance.endOfFrame.then ((_) {
548- if (! mounted) return ;
548+ if (mounted) return _initializeState ();
549+ });
550+ }
549551
550- // Call the listener once to make sure the initial state is reflected
551- // correctly in the UI.
552- _onChangedDebounced.call ();
552+ void _initializeState () {
553+ // Call the listener once to make sure the initial state is reflected
554+ // correctly in the UI.
555+ _onChangedDebounced.call ();
553556
554- final channel = StreamChannel .of (context).channel;
555- final config = StreamChatConfiguration .of (context);
557+ final channel = StreamChannel .of (context).channel;
558+ final config = StreamChatConfiguration .of (context);
556559
557- // Resumes the cooldown if the channel has currently an active cooldown.
558- if (! _isEditing) {
559- _effectiveController.startCooldown (channel.getRemainingCooldown ());
560- }
560+ // Resumes the cooldown if the channel has currently an active cooldown.
561+ if (! _isEditing) {
562+ _effectiveController.startCooldown (channel.getRemainingCooldown ());
563+ }
561564
562- if (config.draftMessagesEnabled) {
563- // Starts listening to the draft stream for the current channel/thread.
564- final draftStream = switch (_effectiveController.message.parentId) {
565- final parentId? => channel.state? .threadDraftStream (parentId),
566- _ => channel.state? .draftStream,
567- };
565+ // Starts listening to the draft stream for the current channel/thread.
566+ if (config.draftMessagesEnabled) {
567+ final draftStream = switch (_effectiveController.message.parentId) {
568+ final parentId? => channel.state? .threadDraftStream (parentId),
569+ _ => channel.state? .draftStream,
570+ };
568571
569- _draftStreamSubscription = draftStream? .distinct ().listen ((draft) {
570- // If the draft is removed, reset the controller.
571- if (draft == null ) return _effectiveController. reset ();
572+ _draftStreamSubscription = draftStream? .distinct ().listen (_onDraftUpdate);
573+ }
574+ }
572575
573- // Otherwise, update the controller with the draft message.
574- if (draft.message case final draftMessage) {
575- _effectiveController.message = draftMessage.toMessage ();
576- }
577- });
578- }
579- });
576+ void _onDraftUpdate (Draft ? draft) {
577+ // If the draft is removed, reset the controller.
578+ if (draft == null ) return _effectiveController.reset ();
579+
580+ // Otherwise, update the controller with the draft message.
581+ if (draft.message case final draftMessage) {
582+ _effectiveController.message = draftMessage.toMessage ();
583+ }
580584 }
581585
582586 @override
0 commit comments