diff --git a/lib/src/channels_bloc.dart b/lib/src/channels_bloc.dart index 9dfafbc2..6acb04cc 100644 --- a/lib/src/channels_bloc.dart +++ b/lib/src/channels_bloc.dart @@ -104,14 +104,15 @@ class ChannelsBlocState extends State } } - StreamSubscription _newMessagesSubscription; + final List _subscriptions = []; @override void initState() { super.initState(); - _newMessagesSubscription = - StreamChat.of(context).client.on(EventType.messageNew).listen((e) { + final client = StreamChat.of(context).client; + + _subscriptions.add(client.on(EventType.messageNew).listen((e) { final newChannels = List.from(channels ?? []); final index = newChannels.indexWhere((c) => c.cid == e.cid); if (index > 0) { @@ -119,14 +120,20 @@ class ChannelsBlocState extends State newChannels.insert(0, channel); _channelsController.add(newChannels); } - }); + })); + + _subscriptions.add(client.on(EventType.channelDeleted).listen((e) { + final channel = e.channel; + _channelsController + .add(channels..removeWhere((c) => c.cid == channel.cid)); + })); } @override void dispose() { _channelsController.close(); _queryChannelsLoadingController.close(); - _newMessagesSubscription.cancel(); + _subscriptions.forEach((s) => s.cancel()); super.dispose(); }