Use async methods synchronously in initState in one of my page

3,270 views
Skip to first unread message

jack

unread,
Aug 31, 2018, 1:25:49 AM8/31/18
to Flutter Dev
Want to call two async methods in my page and execute them one after other(synchronously). Should get into second method after completing first method.

Mikkel Ravn

unread,
Aug 31, 2018, 1:57:38 AM8/31/18
to yashwanth...@gmail.com, Flutter Dev
Something like this?

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

Future<void> _doAsyncStuff() async {
  await firstThing();
  await secondThing();
}

On Fri, Aug 31, 2018 at 7:25 AM jack <yashwanth...@gmail.com> wrote:
Want to call two async methods in my page and execute them one after other(synchronously). Should get into second method after completing first method.

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Mikkel Nygaard Ravn
Software Engineer

jack

unread,
Aug 31, 2018, 2:07:28 AM8/31/18
to Flutter Dev
yeah

more like this 

@override
void initState() {
  super.initState();
  _doAsyncStuff();
  _doAsyncStuff2()
}

Future<void> _doAsyncStuff() async {
  await firstThing();
}

Future<void> _doAsyncStuff2() async {
  await firstThing();
}

Thanks for responding

Mikkel Ravn

unread,
Aug 31, 2018, 2:09:50 AM8/31/18
to yashwanth...@gmail.com, Flutter Dev
_doAsyncStuff2() is not waiting for _doAsyncStuff() to complete here.

jack

unread,
Aug 31, 2018, 2:15:20 AM8/31/18
to Flutter Dev
no. it does not to wait for the first to complete.

Olaide Nojeem Ekeolere

unread,
Aug 31, 2018, 2:27:52 AM8/31/18
to jack, Flutter Dev
Of course it wont wait for the second async method based on your code. You have said in your initState that the first and second async should run without wait for the first to finish. Do as Mikkel said create another async method then call both your methods inside it using await and you will be fine.
Or you could just call the first async method using then like this
aysncMethodOne.then((_)=>asyncMethodTwo()););

jack

unread,
Aug 31, 2018, 2:43:54 AM8/31/18
to Flutter Dev
Will try for that
thanks for responding.
Reply all
Reply to author
Forward
0 new messages