diff --git a/packages/stream_chat_flutter/lib/src/message_list_view.dart b/packages/stream_chat_flutter/lib/src/message_list_view.dart index e07d9358..db14924b 100644 --- a/packages/stream_chat_flutter/lib/src/message_list_view.dart +++ b/packages/stream_chat_flutter/lib/src/message_list_view.dart @@ -570,9 +570,9 @@ class _MessageListViewState extends State { return const SizedBox(); } - var index = _getTopElement(values).index; + var index = _getTopElement(values)?.index; - if (index > messages.length) { + if (index == null || index > messages.length) { return const SizedBox(); } @@ -598,10 +598,17 @@ class _MessageListViewState extends State { StreamChannelState? channel, QueryDirection direction) => _messageListController.paginateData!(direction: direction); - ItemPosition _getTopElement(Iterable values) => values - .where((ItemPosition position) => position.itemLeadingEdge < 0.9) - .reduce((ItemPosition max, ItemPosition position) => - position.itemLeadingEdge > max.itemLeadingEdge ? position : max); + ItemPosition? _getTopElement(Iterable values) { + final inView = + values.where((ItemPosition position) => position.itemLeadingEdge < 0.9); + + if (inView.isEmpty) { + return null; + } + + return inView.reduce((ItemPosition max, ItemPosition position) => + position.itemLeadingEdge > max.itemLeadingEdge ? position : max); + } Widget _buildScrollToBottom() => StreamBuilder>( stream: Rx.combineLatest2(