of static method

ContextStorageState? of(
  1. BuildContext context, [
  2. String? name
])

Returns the closest ContextStorageState which encloses the given context.

In the second parameter, you can pass the name of the storage, which must be found in the widget tree.

Typical usage is as follows:

ContextStorage.of(context).get<int>('pageIndex');
// or
ContextStorage.of(context).set('pageIndex', _pageIndex);

Implementation

static ContextStorageState? of(BuildContext context, [String? name]) {
  if (name != null) {
    final InheritedElement? element = context
        .getElementForInheritedWidgetOfExactType<ContextStorageScope>();
    if (element != null) {
      final ContextStorageScope scope = element.widget as ContextStorageScope;
      final state = scope.state;
      if (state.name == name) return state;

      final parent = ContextStorage.of(state.context, name);
      return parent;
    } else {
      return null;
    }
  } else {
    final InheritedElement? element = context
        .getElementForInheritedWidgetOfExactType<ContextStorageScope>();
    if (element != null) {
      final ContextStorageScope scope = element.widget as ContextStorageScope;
      final state = scope.state;
      return state;
    }
  }
}