Search code examples
flutter

initialize data once in initState and call the setState when data is ready causes exception


Since flutter calls the build method many times in different condition, to avoid getting the data many times, I initialize the data in initState.

I want to re-build the widget when the data is ready.

Here is my code :

class Test extends StatefulWidget {

  @override
  _TestState createState() => new _TestState();

}

class _TestState extends State<Test> {

  Data data;
  bool dataReady = false;

  @override
  void initState() {
    super.initState();

    getData(context).then((Data data) async {
      setState(() {
        dataReady= true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (dataReady) {
      return createMainContent(context);
    } else {
      return new Container();
    }
  }

}

However, it results in following exception :

inheritFromWidgetOfExactType(_InheritedProvider) or inheritFromElement() was called before _TestState.initState() completed.

May I know am I doing something wrong here?

When I add the following line to implementation of getData(context)

     await Future.delayed(new Duration(milliseconds: 300));

the exception does not happen.


Solution

  • Edit: Better answer below.


    Apparently, you cannot access getData(context) during initState (more concrete: before it completed).

    The reason, so I believe, is that getData tries to look up an InheritedWidget ancestor up in the tree, but the tree is just now being built (your widget is created during the parent widget's build).

    The obvious solution would be to delay getData's lookup to a later point in time. There are several ways to achieve that:

    • Delay the lookup to a later time. scheduleMicrotask should work fine.
    • Look it up during the first build call. You could have an isInitialized field set to false and in you build, something like:

      if (!isInitialized) {
        isInitialized = true;
        // TODO: do the getData(...) stuff
      }