of method

T of(
  1. BuildContext context, {
  2. bool listen = true,
})

Accesses this store from the nearest OrbitScope, falling back to the app-wide singleton if no scope is found.

Subscribes this widget context to rebuild whenever this store changes if listen is true (only applicable when resolved via OrbitScope).

Implementation

T of(BuildContext context, {bool listen = true}) {
  if (listen) {
    final scoped = context
        .dependOnInheritedWidgetOfExactType<_OrbitScopeInherited<T>>()
        ?.store;
    if (scoped != null) return scoped;
    assert(() {
      debugPrint(
        'Orbit: $T.of(context) (or context.orbit($T)) was called with '
        'listen: true (the default), but no OrbitScope<$T> was found '
        'above this context — so it fell back to the app-wide singleton '
        'WITHOUT actually subscribing this widget to rebuild on changes. '
        'This is a silent one-time read, not a reactive watch. Wrap the '
        'relevant widget in OrbitScope<$T>(...) for scoped reactivity, '
        'or use OrbitBuilder<$T>/OrbitSelector<$T, ...> to reactively '
        'watch the global singleton instead.',
      );
      return true;
    }());
  } else {
    final element = context
        .getElementForInheritedWidgetOfExactType<_OrbitScopeInherited<T>>();
    if (element != null) {
      return (element.widget as _OrbitScopeInherited<T>).store;
    }
  }
  return Orbit.use<T>(_create);
}