async x await x Future

Asked

Viewed 1,039 times

-1

What are Keywords for async and await in Flutter?

Some doubts I have are:

1 - They belong to Flutter or Dart?

2 - Why put the async at the end of the following method?

 void _method(TodoItem item) async { }

3 - Why put the await at the beginning of the call of the following method?

_results = await DB.query(TodoItem.table);

4 - What difference between async and await?

5 - Where the Future gets in the middle of it all?

  • 1

    Damn, this site is only for professionals!? Take it easy, seems to already go negative and voting to close without even reading the question

  • 1

    @rubStackOverflow yesterday had a discussion about it that you commented on another question hahahaha Ta difficult, mainly the tag Flutter (It seems to be a problem tag), ta right that many questions from Flutter the galley neither gives to the luxury of understanding how to ask a question here, but I don’t think it’s valid to go downvote at all... Even more the one that adds knowledge to the site (As they say it should be).

  • 1

    @Matheusribeiro I saw, downvote is trivialized.

1 answer

8

Dart, like other languages, works with synchronous and asynchronous functions.

I’ll explain in a grosser way, but then you research more on:

Synchronous Function

bool tarefaFinalizada() => tarefas[0].finalizada == true;

It is a function that when the execution of your system reaches it, it will wait for everything within it to be executed, to continue doing what it needs.

Asynchronous Function

Future<bool> getTarefas() async => await http.get('tarefas');

It is a function that the system performs, but does not wait for its termination, it performs this function and continues to do what it needs.

That is, the synchronous function, you expect the response of your function to perform certain action.

Future

This function will return a data from the future ~~Beware of Skynet

When you use asynchronous functions, you use the Future to indicate that that function is asynchronous, or rather that it will return a value in the "future", for example, will return the values in 5 seconds.

await (Translating to BR -> "Wait")

Wait here until the function ends and get the result of it.

You use the await when you make a call to return a future. This causes the system to wait for the asynchronous function call to finish so that it runs the rest of the things. That is, it transforms an asynchronous function into synchronous. Whenever you use a function that returns a Future you will have to, at some point, add the await to receive the correct value, otherwise you will always receive a Future<SeuTipo>.

async

This function is asynchronous and at some point I may need to wait to pick up some data.

You need to use the async in the functions where you will use the await to indicate that the method will deal with asynchronous functions synchronously.

You can read a little more here on these links:

My advice...

Study more about programming and especially study more about the Dart language and the Flutter framework!

Flutter is a fast and practical framework, but for those who already know a little about programming. It is better to start already understanding and doing things correctly, than to go tinkering as you learn... So train, test, ALWAYS READ the Flutter documentation that is very well made and organized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.