of<S, E> static method

Kokoro<S, E> of<S, E>(
  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 Kokoro<S, E> of<S, E>(BuildContext context, {bool listen = true}) {
  final provider = (listen
      ? context.dependOnInheritedWidgetOfExactType<AppProvider<S, E>>()
      : context
          .getElementForInheritedWidgetOfExactType<AppProvider<S, E>>()
          ?.widget) as AppProvider<S, E>?;

  if (provider == null) throw AppProviderError<AppProvider<S, E>>();

  return provider._app;
}