Skip to content

Commit a6e0efd

Browse files
committed
msglist [nfc]: Simplify a bit by centralizing model! null-assertion
1 parent 86e2397 commit a6e0efd

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/widgets/message_list.dart

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,11 @@ class MessageList extends StatefulWidget {
448448
}
449449

450450
class _MessageListState extends State<MessageList> with PerAccountStoreAwareStateMixin<MessageList> {
451-
MessageListView? model;
451+
MessageListView get model => _model!;
452+
MessageListView? _model;
453+
452454
final MessageListScrollController scrollController = MessageListScrollController();
455+
453456
final ValueNotifier<bool> _scrollToBottomVisible = ValueNotifier<bool>(false);
454457

455458
@override
@@ -460,32 +463,32 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
460463

461464
@override
462465
void onNewStore() { // TODO(#464) try to keep using old model until new one gets messages
463-
model?.dispose();
466+
_model?.dispose();
464467
_initModel(PerAccountStoreWidget.of(context));
465468
}
466469

467470
@override
468471
void dispose() {
469-
model?.dispose();
472+
_model?.dispose();
470473
scrollController.dispose();
471474
_scrollToBottomVisible.dispose();
472475
super.dispose();
473476
}
474477

475478
void _initModel(PerAccountStore store) {
476-
model = MessageListView.init(store: store, narrow: widget.narrow);
477-
model!.addListener(_modelChanged);
478-
model!.fetchInitial();
479+
_model = MessageListView.init(store: store, narrow: widget.narrow);
480+
model.addListener(_modelChanged);
481+
model.fetchInitial();
479482
}
480483

481484
void _modelChanged() {
482-
if (model!.narrow != widget.narrow) {
485+
if (model.narrow != widget.narrow) {
483486
// Either:
484487
// - A message move event occurred, where propagate mode is
485488
// [PropagateMode.changeAll] or [PropagateMode.changeLater]. Or:
486489
// - We fetched a "with" / topic-permalink narrow, and the response
487490
// redirected us to the new location of the operand message ID.
488-
widget.onNarrowChanged(model!.narrow);
491+
widget.onNarrowChanged(model.narrow);
489492
}
490493
setState(() {
491494
// The actual state lives in the [MessageListView] model.
@@ -507,7 +510,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
507510
// but makes things a bit more complicated to reason about.
508511
// The cause seems to be that this gets called again with maxScrollExtent
509512
// still not yet updated to account for the newly-added messages.
510-
model!.fetchOlder();
513+
model.fetchOlder();
511514
}
512515
}
513516

@@ -528,8 +531,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
528531

529532
@override
530533
Widget build(BuildContext context) {
531-
assert(model != null);
532-
if (!model!.fetched) return const Center(child: CircularProgressIndicator());
534+
if (!model.fetched) return const Center(child: CircularProgressIndicator());
533535

534536
// Pad the left and right insets, for small devices in landscape.
535537
return SafeArea(
@@ -571,9 +573,9 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
571573

572574
// The list has two slivers: a top sliver growing upward,
573575
// and a bottom sliver growing downward.
574-
// Each sliver has some of the items from `model!.items`.
576+
// Each sliver has some of the items from `model.items`.
575577
const maxBottomItems = 1;
576-
final totalItems = model!.items.length;
578+
final totalItems = model.items.length;
577579
final bottomItems = totalItems <= maxBottomItems ? totalItems : maxBottomItems;
578580
final topItems = totalItems - bottomItems;
579581

@@ -599,7 +601,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
599601
// and will not trigger this callback.
600602
findChildIndexCallback: (Key key) {
601603
final messageId = (key as ValueKey<int>).value;
602-
final itemIndex = model!.findItemWithMessageId(messageId);
604+
final itemIndex = model.findItemWithMessageId(messageId);
603605
if (itemIndex == -1) return null;
604606
final childIndex = totalItems - 1 - (itemIndex + bottomItems);
605607
if (childIndex < 0) return null;
@@ -608,7 +610,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
608610
childCount: topItems,
609611
(context, childIndex) {
610612
final itemIndex = totalItems - 1 - (childIndex + bottomItems);
611-
final data = model!.items[itemIndex];
613+
final data = model.items[itemIndex];
612614
final item = _buildItem(zulipLocalizations, data);
613615
return item;
614616
}));
@@ -637,7 +639,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
637639
// and will not trigger this callback.
638640
findChildIndexCallback: (Key key) {
639641
final messageId = (key as ValueKey<int>).value;
640-
final itemIndex = model!.findItemWithMessageId(messageId);
642+
final itemIndex = model.findItemWithMessageId(messageId);
641643
if (itemIndex == -1) return null;
642644
final childIndex = itemIndex - topItems;
643645
if (childIndex < 0) return null;
@@ -654,7 +656,7 @@ class _MessageListState extends State<MessageList> with PerAccountStoreAwareStat
654656
if (childIndex == bottomItems) return TypingStatusWidget(narrow: widget.narrow);
655657

656658
final itemIndex = topItems + childIndex;
657-
final data = model!.items[itemIndex];
659+
final data = model.items[itemIndex];
658660
return _buildItem(zulipLocalizations, data);
659661
}));
660662

0 commit comments

Comments
 (0)