2

Flutter - Is there a way to load async metho on InitState? i want to call async method in iniState(); but it is not worked...

I'm a looking for a way to load async data on InitState method, I need some data before build method runs. I need to execute build method 'till a Stream runs.

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

         //here i want to call async function
      }
idea point
  • 31
  • 6
  • 2
    Does this answer your question? [Is there a way to load async data on InitState method?](https://stackoverflow.com/questions/51901002/is-there-a-way-to-load-async-data-on-initstate-method) – Simon Sot Jun 16 '21 at 18:13

2 Answers2

1

You can create a separate method and can call in the initState(). Moreover you can use WidgetBinding to call the function like this.

void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_)  => {
      your_method();
    // TODO: implement initState
    super.initState();
  }
Muhammad Ashir
  • 418
  • 2
  • 14
1

You could try with a below code snippet.

Method 1 : Create an async method and call it from you initState() method like shown below:

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

   void asyncMethod() async {
   await asyncCall1();
   await asyncCall2();
   // ....
  }
Kalpesh Khandla
  • 758
  • 1
  • 9
  • 22