Skip to content

Commit 6ec1def

Browse files
committed
msglist [nfc]: Add MessageListPage.maybeAncestorOf, alongside .ancestorOf
1 parent 7f7aa98 commit 6ec1def

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

lib/widgets/message_list.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,30 @@ class MessageListPage extends StatefulWidget {
197197
///
198198
/// Uses the inefficient [BuildContext.findAncestorStateOfType];
199199
/// don't call this in a build method.
200-
// If we do find ourselves wanting this in a build method, it won't be hard
201-
// to enable that: we'd just need to add an [InheritedWidget] here.
200+
///
201+
/// See also:
202+
/// * [maybeAncestorOf], which returns null instead of throwing
203+
/// when an ancestor [MessageListPageState] is not found.
202204
static MessageListPageState ancestorOf(BuildContext context) {
203-
final state = context.findAncestorStateOfType<_MessageListPageState>();
205+
final state = maybeAncestorOf(context);
204206
assert(state != null, 'No MessageListPage ancestor');
205207
return state!;
206208
}
207209

210+
/// The [MessageListPageState] above this context in the tree, if any.
211+
///
212+
/// Uses the inefficient [BuildContext.findAncestorStateOfType];
213+
/// don't call this in a build method.
214+
///
215+
/// See also:
216+
/// * [ancestorOf], which throws instead of returning null
217+
/// when an ancestor [MessageListPageState] is not found.
218+
// If we do find ourselves wanting this in a build method, it won't be hard
219+
// to enable that: we'd just need to add an [InheritedWidget] here.
220+
static MessageListPageState? maybeAncestorOf(BuildContext context) {
221+
return context.findAncestorStateOfType<_MessageListPageState>();
222+
}
223+
208224
final Narrow initNarrow;
209225
final int? initAnchorMessageId; // TODO(#1564) highlight target upon load
210226

0 commit comments

Comments
 (0)