of static method

Nexus of(
  1. BuildContext context
)

Implementation

static Nexus of(BuildContext context) {
  // `context.nexus` must be safe from ANY widget lifecycle method — including
  // initState() and dispose(), where looking up an inherited ancestor throws
  // ("deactivated widget's ancestor is unsafe" / "called before initState
  // completed"). The Nexus instance is a global singleton, so the NexusScope
  // override is only a nicety: consult it while the element is mounted (no
  // rebuild dependency is registered), and fall back to the global instance
  // in every other case.
  if (context.mounted) {
    try {
      final scope = context.getInheritedWidgetOfExactType<_NexusInherited>();
      if (scope != null) return scope.nexus;
    } catch (_) {
      /* unsafe lookup context — fall through to the global instance */
    }
  }
  return Nexus.instance;
}