rozerro
rozerro

Reputation: 7125

Using a fuction which returns Future as usual function

When we run a function which returns Future without await - will it execute synchronously?

Here is UserPreferences.setUser(newUser); returns Future.

Will this function wait the ending of setUser i.e. will it runs as synchronous function? Or will the result of setUser be asynchronous?

return AppBar(
    ...
    actions: [
      ThemeSwitcher(
        builder: (context) => IconButton(
          icon: Icon(icon),
          onPressed: () {
            ...
            // this returns a Future
            UserPreferences.setUser(newUser);
          },
        ),
      ),
    ],
  );
}

Upvotes: 0

Views: 44

Answers (1)

Jorge Vieira
Jorge Vieira

Reputation: 3064

It will run async and will give you the result at anytime.

You can do like this, blocking the code execution until the function finishes:

String result = await UserPreferences.setUser();

Or you can do like this, not blocking the code execution and when it finishes the result will be given:

UserPreferences.setUser().then((value){
  String result = value;
});

Upvotes: 1

Related Questions