Skip to content

Commit ffd96fe

Browse files
committed
msglist [nfc]: Add MessageListPage.maybeAncestorOf, alongside .ancestorOf
1 parent edeb05d commit ffd96fe

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
@@ -206,14 +206,30 @@ class MessageListPage extends StatefulWidget {
206206
///
207207
/// Uses the inefficient [BuildContext.findAncestorStateOfType];
208208
/// don't call this in a build method.
209-
// If we do find ourselves wanting this in a build method, it won't be hard
210-
// to enable that: we'd just need to add an [InheritedWidget] here.
209+
///
210+
/// See also:
211+
/// * [maybeAncestorOf], which returns null instead of throwing
212+
/// when an ancestor [MessageListPageState] is not found.
211213
static MessageListPageState ancestorOf(BuildContext context) {
212-
final state = context.findAncestorStateOfType<_MessageListPageState>();
214+
final state = maybeAncestorOf(context);
213215
assert(state != null, 'No MessageListPage ancestor');
214216
return state!;
215217
}
216218

219+
/// The [MessageListPageState] above this context in the tree, if any.
220+
///
221+
/// Uses the inefficient [BuildContext.findAncestorStateOfType];
222+
/// don't call this in a build method.
223+
///
224+
/// See also:
225+
/// * [ancestorOf], which throws instead of returning null
226+
/// when an ancestor [MessageListPageState] is not found.
227+
// If we do find ourselves wanting this in a build method, it won't be hard
228+
// to enable that: we'd just need to add an [InheritedWidget] here.
229+
static MessageListPageState? maybeAncestorOf(BuildContext context) {
230+
return context.findAncestorStateOfType<_MessageListPageState>();
231+
}
232+
217233
final Narrow initNarrow;
218234
final int? initAnchorMessageId; // TODO(#1564) highlight target upon load
219235

0 commit comments

Comments
 (0)