Skip to content

Commit 7f2f68f

Browse files
committed
fix: Use math.max to prevent negative startIndex
The `startIndex` for the `StreamFullScreenMediaBuilder` could potentially be -1 if the attachment is not found in the list. This change ensures that `startIndex` is never negative by using `math.max(0, startIndex)`, preventing potential crashes.
1 parent 6a72c9c commit 7f2f68f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

packages/stream_chat_flutter/lib/src/message_widget/parse_attachments.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:math' as math;
23

34
import 'package:flutter/material.dart';
45
import 'package:stream_chat_flutter/src/attachment/attachment_widget_catalog.dart';
@@ -144,6 +145,9 @@ class ParseAttachments extends StatelessWidget {
144145

145146
final navigator = Navigator.of(context);
146147
final channel = StreamChannel.of(context).channel;
148+
final startIndex = attachments.indexWhere(
149+
(it) => it.attachment.id == attachment.id,
150+
);
147151

148152
return navigator.push<void>(
149153
MaterialPageRoute(
@@ -152,9 +156,7 @@ class ParseAttachments extends StatelessWidget {
152156
child: StreamFullScreenMediaBuilder(
153157
userName: message.user!.name,
154158
mediaAttachmentPackages: attachments,
155-
startIndex: attachments.indexWhere(
156-
(it) => it.attachment.id == attachment.id,
157-
),
159+
startIndex: math.max(0, startIndex),
158160
onReplyMessage: onReplyTap,
159161
onShowMessage: onShowMessage,
160162
attachmentActionsModalBuilder: attachmentActionsModalBuilder,

0 commit comments

Comments
 (0)