Is there a difference whether using await with return in Dart?

322

Since you are returning Future, in this exact case, even if you don`t await it, the foo(3) will return Future instead and it is the exact thing your function wants to return. and if u await it there, then it will await in the function and then return the value.

you don`t even need await in this case if foo return Future. Then the caller of bar will instead await the bar.

Share:
322
ArtS
Author by

ArtS

Updated on December 25, 2022

Comments

  • ArtS
    ArtS 11 months

    In my flutter project, say there's a foo(int x) async function. And bar() async that looks like:

    Future bar() async {
      return foo(3);
    }
    

    The bar() function is just a simple capsule for foo(int x) with a certain parameter. And bar() is used as the future in a FutureBuilder.

    I'm not sure if I should use await with return. I can't find related documentations online. So what's the difference between return foo(3) and return await foo(3)? Thank you!