didChangeDependencies method

  1. @override
void didChangeDependencies()
override

Called when a dependency of this State object changes.

For example, if the previous call to build referenced an InheritedWidget that later changed, the framework would call this method to notify this object about the change.

This method is also called immediately after initState. It is safe to call BuildContext.dependOnInheritedWidgetOfExactType from this method.

Subclasses rarely override this method because the framework always calls build after a dependency changes. Some subclasses do override this method because they need to do some expensive work (e.g., network fetches) when their dependencies change, and that work would be too expensive to do for every build.

Implementation

@override
void didChangeDependencies() {
  final newStreamChatCoreState = StreamChatCore.of(context);

  if (newStreamChatCoreState != _streamChatCoreState) {
    _streamChatCoreState = newStreamChatCoreState;
    final client = _streamChatCoreState!.client;

    _cancelSubscriptions();
    if (!widget.lockChannelsOrder) {
      _subscriptions.add(client
          .on(
        EventType.messageNew,
      )
          .listen((e) {
        if (e.message?.parentId != null && e.message?.showInChannel != true) {
          return;
        }
        final newChannels = List<Channel>.from(channels ?? []);
        final index = newChannels.indexWhere((c) => c.cid == e.cid);
        if (index != -1) {
          if (index > 0) {
            final channel = newChannels.removeAt(index);
            newChannels.insert(0, channel);
          }
        } else if (widget.shouldAddChannel?.call(e) == true) {
          final hiddenIndex =
              _hiddenChannels.indexWhere((c) => c.cid == e.cid);
          if (hiddenIndex != -1) {
            newChannels.insert(0, _hiddenChannels[hiddenIndex]);
            _hiddenChannels.removeAt(hiddenIndex);
          } else {
            if (client.state.channels[e.cid] != null) {
              newChannels.insert(0, client.state.channels[e.cid]!);
            }
          }
        }

        if (widget.channelsComparator != null) {
          newChannels.sort(widget.channelsComparator);
        }
        _channelsController.safeAdd(newChannels);
      }));
    }

    _subscriptions
      ..add(client.on(EventType.channelHidden).listen((event) async {
        final newChannels = List<Channel>.from(channels ?? []);
        final channelIndex =
            newChannels.indexWhere((c) => c.cid == event.cid);
        if (channelIndex > -1) {
          final channel = newChannels.removeAt(channelIndex);
          _hiddenChannels.add(channel);
          _channelsController.safeAdd(newChannels);
        }
      }))
      ..add(client
          .on(
        EventType.channelDeleted,
        EventType.notificationRemovedFromChannel,
      )
          .listen((e) {
        final channel = e.channel;
        _channelsController.safeAdd(List.from(
          (channels ?? [])..removeWhere((c) => c.cid == channel?.cid),
        ));
      }));
  }

  super.didChangeDependencies();
}