@@ -665,7 +665,7 @@ class Channel {
665
665
_checkInitialized ();
666
666
667
667
// Clean up stale error messages before sending a new message.
668
- state! .cleanUpStaleErrorMessages ();
668
+ state? .cleanUpStaleErrorMessages ();
669
669
670
670
// Cancelling previous completer in case it's called again in the process
671
671
// Eg. Updating the message while the previous call is in progress.
@@ -690,7 +690,7 @@ class Channel {
690
690
).toList (),
691
691
);
692
692
693
- state! .updateMessage (message);
693
+ state? .updateMessage (message);
694
694
695
695
try {
696
696
if (message.attachments.any ((it) => ! it.uploadState.isSuccess)) {
@@ -724,20 +724,22 @@ class Channel {
724
724
state: MessageState .sent,
725
725
);
726
726
727
- state! .updateMessage (sentMessage);
727
+ state? .updateMessage (sentMessage);
728
728
729
729
return response;
730
730
} catch (e) {
731
+ final failedMessage = message.copyWith (
732
+ // Update the message state to failed.
733
+ state: MessageState .sendingFailed (
734
+ skipPush: skipPush,
735
+ skipEnrichUrl: skipEnrichUrl,
736
+ ),
737
+ );
738
+
739
+ state? .updateMessage (failedMessage);
740
+ // If the error is retriable, add it to the retry queue.
731
741
if (e is StreamChatNetworkError && e.isRetriable) {
732
- state! ._retryQueue.add ([
733
- message.copyWith (
734
- // Update the message state to failed.
735
- state: MessageState .sendingFailed (
736
- skipPush: skipPush,
737
- skipEnrichUrl: skipEnrichUrl,
738
- ),
739
- ),
740
- ]);
742
+ state? ._retryQueue.add ([failedMessage]);
741
743
}
742
744
743
745
rethrow ;
@@ -756,7 +758,6 @@ class Channel {
756
758
bool skipEnrichUrl = false ,
757
759
}) async {
758
760
_checkInitialized ();
759
- final originalMessage = message;
760
761
761
762
// Cancelling previous completer in case it's called again in the process
762
763
// Eg. Updating the message while the previous call is in progress.
@@ -813,28 +814,20 @@ class Channel {
813
814
814
815
return response;
815
816
} catch (e) {
816
- if (e is StreamChatNetworkError ) {
817
- if (e.isRetriable) {
818
- state! ._retryQueue.add ([
819
- message.copyWith (
820
- // Update the message state to failed.
821
- state: MessageState .updatingFailed (
822
- skipPush: skipPush,
823
- skipEnrichUrl: skipEnrichUrl,
824
- ),
825
- ),
826
- ]);
827
- } else {
828
- // Reset the message to original state if the update fails and is not
829
- // retriable.
830
- state? .updateMessage (originalMessage.copyWith (
831
- state: MessageState .updatingFailed (
832
- skipPush: skipPush,
833
- skipEnrichUrl: skipEnrichUrl,
834
- ),
835
- ));
836
- }
817
+ final failedMessage = message.copyWith (
818
+ // Update the message state to failed.
819
+ state: MessageState .updatingFailed (
820
+ skipPush: skipPush,
821
+ skipEnrichUrl: skipEnrichUrl,
822
+ ),
823
+ );
824
+
825
+ state? .updateMessage (failedMessage);
826
+ // If the error is retriable, add it to the retry queue.
827
+ if (e is StreamChatNetworkError && e.isRetriable) {
828
+ state? ._retryQueue.add ([failedMessage]);
837
829
}
830
+
838
831
rethrow ;
839
832
}
840
833
}
@@ -851,7 +844,6 @@ class Channel {
851
844
bool skipEnrichUrl = false ,
852
845
}) async {
853
846
_checkInitialized ();
854
- final originalMessage = message;
855
847
856
848
// Cancelling previous completer in case it's called again in the process
857
849
// Eg. Updating the message while the previous call is in progress.
@@ -889,31 +881,19 @@ class Channel {
889
881
890
882
return response;
891
883
} catch (e) {
892
- if (e is StreamChatNetworkError ) {
893
- if (e.isRetriable) {
894
- state! ._retryQueue.add ([
895
- message.copyWith (
896
- // Update the message state to failed.
897
- state: MessageState .partialUpdatingFailed (
898
- set : set ,
899
- unset: unset,
900
- skipEnrichUrl: skipEnrichUrl,
901
- ),
902
- ),
903
- ]);
904
- } else {
905
- // Reset the message to original state if the update fails and is not
906
- // retriable.
907
- state? .updateMessage (
908
- originalMessage.copyWith (
909
- state: MessageState .partialUpdatingFailed (
910
- set : set ,
911
- unset: unset,
912
- skipEnrichUrl: skipEnrichUrl,
913
- ),
914
- ),
915
- );
916
- }
884
+ final failedMessage = message.copyWith (
885
+ // Update the message state to failed.
886
+ state: MessageState .partialUpdatingFailed (
887
+ set : set ,
888
+ unset: unset,
889
+ skipEnrichUrl: skipEnrichUrl,
890
+ ),
891
+ );
892
+
893
+ state? .updateMessage (failedMessage);
894
+ // If the error is retriable, add it to the retry queue.
895
+ if (e is StreamChatNetworkError && e.isRetriable) {
896
+ state? ._retryQueue.add ([failedMessage]);
917
897
}
918
898
919
899
rethrow ;
@@ -932,7 +912,7 @@ class Channel {
932
912
// Directly deleting the local messages and bounced error messages as they
933
913
// are not available on the server.
934
914
if (message.remoteCreatedAt == null || message.isBouncedWithError) {
935
- state! .deleteMessage (
915
+ state? .deleteMessage (
936
916
message.copyWith (
937
917
type: MessageType .deleted,
938
918
localDeletedAt: DateTime .now (),
@@ -987,14 +967,17 @@ class Channel {
987
967
988
968
return response;
989
969
} catch (e) {
970
+ final failedMessage = message.copyWith (
971
+ // Update the message state to failed.
972
+ state: MessageState .deletingFailed (hard: hard),
973
+ );
974
+
975
+ state? .updateMessage (failedMessage);
976
+ // If the error is retriable, add it to the retry queue.
990
977
if (e is StreamChatNetworkError && e.isRetriable) {
991
- state! ._retryQueue.add ([
992
- message.copyWith (
993
- // Update the message state to failed.
994
- state: MessageState .deletingFailed (hard: hard),
995
- ),
996
- ]);
978
+ state? ._retryQueue.add ([failedMessage]);
997
979
}
980
+
998
981
rethrow ;
999
982
}
1000
983
}
@@ -1005,6 +988,9 @@ class Channel {
1005
988
/// retry action:
1006
989
/// - For [MessageState.sendingFailed] , it attempts to send the message.
1007
990
/// - For [MessageState.updatingFailed] , it attempts to update the message.
991
+ /// - For [MessageState.partialUpdatingFailed] , it attempts to partially
992
+ /// update the message with the same 'set' and 'unset' parameters that were
993
+ /// used in the original request.
1008
994
/// - For [MessageState.deletingFailed] , it attempts to delete the message.
1009
995
/// with the same 'hard' parameter that was used in the original request
1010
996
/// - For messages with [isBouncedWithError] , it attempts to send the message.
@@ -1026,13 +1012,14 @@ class Channel {
1026
1012
skipPush: skipPush,
1027
1013
skipEnrichUrl: skipEnrichUrl,
1028
1014
),
1029
- partialUpdatingFailed: (set , unset, skipEnrichUrl) =>
1030
- partialUpdateMessage (
1031
- message,
1032
- set : set ,
1033
- unset: unset,
1034
- skipEnrichUrl: skipEnrichUrl,
1035
- ),
1015
+ partialUpdatingFailed: (set , unset, skipEnrichUrl) {
1016
+ return partialUpdateMessage (
1017
+ message,
1018
+ set : set ,
1019
+ unset: unset,
1020
+ skipEnrichUrl: skipEnrichUrl,
1021
+ );
1022
+ },
1036
1023
deletingFailed: (hard) => deleteMessage (message, hard: hard),
1037
1024
),
1038
1025
orElse: () {
@@ -2516,8 +2503,10 @@ class ChannelClientState {
2516
2503
2517
2504
/// Retry failed message.
2518
2505
Future <void > retryFailedMessages () async {
2519
- final failedMessages = [...messages, ...threads.values.expand ((v) => v)]
2520
- .where ((it) => it.state.isFailed);
2506
+ final allMessages = [...messages, ...threads.values.flattened];
2507
+ final failedMessages = allMessages.where ((it) => it.state.isFailed);
2508
+
2509
+ if (failedMessages.isEmpty) return ;
2521
2510
_retryQueue.add (failedMessages);
2522
2511
}
2523
2512
0 commit comments