of<S> static method

Store<S> of<S>(
  1. BuildContext context, {
  2. bool listen = true,
})

A method that can be called by descendant Widgets to retrieve the Store from the StoreProvider.

Important: When using this method, pass through complete type information or Flutter will be unable to find the correct StoreProvider!

Example

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final store = StoreProvider.of<int>(context);

    return Text('${store.state}');
  }
}

If you need to use the Store from the initState function, set the listen option to false.

Example

class MyWidget extends StatefulWidget {
  static GlobalKey<_MyWidgetState> captorKey = GlobalKey<_MyWidgetState>();

  MyWidget() : super(key: captorKey);

  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  Store<String> store;

  @override
  void initState() {
    super.initState();
    store = StoreProvider.of<String>(context, listen: false);
  }

  @override
 Widget build(BuildContext context) {
    return Container();
  }
}

Implementation

static Store<S> of<S>(BuildContext context, {bool listen = true}) {
  final provider = (listen
      ? context.dependOnInheritedWidgetOfExactType<StoreProvider<S>>()
      : context
          .getElementForInheritedWidgetOfExactType<StoreProvider<S>>()
          ?.widget) as StoreProvider<S>?;

  if (provider == null) throw StoreProviderError<StoreProvider<S>>();

  return provider._store;
}