From b2d906c9494b095c95a8c876ccb6076b56536703 Mon Sep 17 00:00:00 2001 From: River Huang Date: Fri, 18 Apr 2025 19:14:03 +1000 Subject: [PATCH 1/2] fix: [Flutter Web] TypeError JSArray --- .../stream_chat/lib/src/client/channel.dart | 47 +++++++++++-------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 994507ace..d4e96fceb 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -566,9 +566,11 @@ class Channel { // update or remove attachment from message. final List newAttachments; if (remove) { - newAttachments = [...message!.attachments]..removeAt(index); + newAttachments = [...message!.attachments] + ..removeAt(index); } else { - newAttachments = [...message!.attachments]..[index] = attachment; + newAttachments = [...message!.attachments]..[index] = + attachment; } final updatedMessage = message!.copyWith(attachments: newAttachments); @@ -1307,7 +1309,9 @@ class Channel { final now = DateTime.now(); final user = _client.state.currentUser; - var latestReactions = [...message.latestReactions ?? []]; + var latestReactions = [ + ...message.latestReactions ?? [] + ]; if (enforceUnique) { latestReactions.removeWhere((it) => it.userId == user!.id); } @@ -1382,15 +1386,17 @@ class Channel { reactionScores.update(type, (value) => value - 1); } - final latestReactions = [...?message.latestReactions]..removeWhere((r) => - r.userId == reaction.userId && - r.type == reaction.type && - r.messageId == reaction.messageId); + final latestReactions = [...?message.latestReactions] + ..removeWhere((r) => + r.userId == reaction.userId && + r.type == reaction.type && + r.messageId == reaction.messageId); - final ownReactions = [...?message.ownReactions]..removeWhere((r) => - r.userId == reaction.userId && - r.type == reaction.type && - r.messageId == reaction.messageId); + final ownReactions = [...?message.ownReactions]..removeWhere( + (r) => + r.userId == reaction.userId && + r.type == reaction.type && + r.messageId == reaction.messageId); final newMessage = message.copyWith( reactionCounts: reactionCounts..removeWhere((_, value) => value == 0), @@ -2235,7 +2241,7 @@ class ChannelClientState { final user = event.user; if (user == null) return; - final existingMembers = [...?channelState.members]; + final existingMembers = [...?channelState.members]; final existingMembership = channelState.membership; // Return if the user is not a existing member of the channel. @@ -2371,7 +2377,7 @@ class ChannelClientState { } void _updateMember(Member member) { - final currentMembers = [...members]; + final currentMembers = [...members]; final memberIndex = currentMembers.indexWhere( (m) => m.userId == member.userId, ); @@ -2405,8 +2411,10 @@ class ChannelClientState { /// Retry failed message. Future retryFailedMessages() async { - final failedMessages = [...messages, ...threads.values.expand((v) => v)] - .where((it) => it.state.isFailed); + final failedMessages = [ + ...messages, + ...threads.values.expand((v) => v) + ].where((it) => it.state.isFailed); _retryQueue.add(failedMessages); } @@ -2787,7 +2795,7 @@ class ChannelClientState { if (message.parentId == null || message.showInChannel == true) { // Create a new list of messages to avoid modifying the original // list directly. - var newMessages = [...messages]; + var newMessages = [...messages]; final oldIndex = newMessages.indexWhere((m) => m.id == message.id); if (oldIndex != -1) { @@ -2869,7 +2877,7 @@ class ChannelClientState { /// Updates the list of pinned messages based on the current message's /// pinned status. List _updatePinnedMessages(Message message) { - final newPinnedMessages = [...pinnedMessages]; + final newPinnedMessages = [...pinnedMessages]; final oldPinnedIndex = newPinnedMessages.indexWhere((m) => m.id == message.id); @@ -2914,10 +2922,11 @@ class ChannelClientState { } // Remove regular message, thread message shown in channel - var updatedMessages = [...messages]..removeWhere((e) => e.id == message.id); + var updatedMessages = [...messages] + ..removeWhere((e) => e.id == message.id); // Remove quoted message reference from every message if available. - updatedMessages = [...updatedMessages].map((it) { + updatedMessages = [...updatedMessages].map((it) { // Early return if the message doesn't have a quoted message. if (it.quotedMessageId != message.id) return it; From 9391452598b3559cd68bf053991567b471ca5fdf Mon Sep 17 00:00:00 2001 From: River Huang Date: Tue, 22 Apr 2025 09:12:43 +1000 Subject: [PATCH 2/2] workaround: silent json error --- packages/stream_chat/lib/src/core/api/channel_api.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/stream_chat/lib/src/core/api/channel_api.dart b/packages/stream_chat/lib/src/core/api/channel_api.dart index f87b99d53..7058b9db7 100644 --- a/packages/stream_chat/lib/src/core/api/channel_api.dart +++ b/packages/stream_chat/lib/src/core/api/channel_api.dart @@ -8,6 +8,7 @@ import 'package:stream_chat/src/core/models/channel_state.dart'; import 'package:stream_chat/src/core/models/event.dart'; import 'package:stream_chat/src/core/models/filter.dart'; import 'package:stream_chat/src/core/models/message.dart'; +import 'package:flutter/foundation.dart'; /// Defines the api dedicated to channel operations class ChannelApi { @@ -79,7 +80,13 @@ class ChannelApi { }), }, ); - return QueryChannelsResponse.fromJson(response.data); + + try { + return QueryChannelsResponse.fromJson(response.data); + } catch (e) { + debugPrint('queryChannels error: $e'); + return QueryChannelsResponse.fromJson({}); + } } /// Mark all channels for this user as read