Skip to content

Commit 47ebab0

Browse files
committed
store [nfc]: Add PerAccountStoreWidget.maybeOf
1 parent 3168034 commit 47ebab0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/widgets/store.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ class PerAccountStoreWidget extends StatefulWidget {
203203
/// on the server.
204204
///
205205
/// See also:
206+
/// * [maybeOf], for a version of this that returns null instead of erroring
207+
/// if there is no [PerAccountStoreWidget] ancestor.
206208
/// * [accountIdOf], for the account ID corresponding to the same data.
207209
/// * [GlobalStoreWidget.of], for the app's data beyond that of a
208210
/// particular account.
@@ -213,6 +215,21 @@ class PerAccountStoreWidget extends StatefulWidget {
213215
return widget!.store;
214216
}
215217

218+
/// Like [of], but returns null instead of erroring if there is no
219+
/// [PerAccountStoreWidget] ancestor.
220+
///
221+
/// Useful in particular for content widgets that have special functionality
222+
/// when they appear in a per-account context, which is the common case
223+
/// (the message list).
224+
///
225+
/// This will be useful for #488, supporting Zulip content rendering
226+
/// in e.g. an org description shown before you log in.
227+
// TODO(#488) Remove #488 comment.
228+
static PerAccountStore? maybeOf(BuildContext context) {
229+
final widget = context.dependOnInheritedWidgetOfExactType<_PerAccountStoreInheritedWidget>();
230+
return widget?.store;
231+
}
232+
216233
/// Our account ID for the relevant account for this widget.
217234
///
218235
/// As with [of], the data is taken from the closest [PerAccountStoreWidget]

0 commit comments

Comments
 (0)