Home > database >  The difference between async, async* and sync* in Flutter
The difference between async, async* and sync* in Flutter

Time:10-02

I know there are async, async* and sync* in Flutter. However, I only know when and how to use async, not async* and sync*.

I know that I need to mark async for the function when the function body has await, but when to mark async* or sync*?

What is the difference between these three? Also, when and how to use them? It would be better if you could share some sample code about async, async* and sync*.

Thanks a lot if someone can tell me what the difference is. Thank you in advance!

CodePudding user response:

See the Dart language tour about Generators: https://dart.dev/guides/language/language-tour#generators

In short, sync* creates an Iterable, and async* creates a Stream, and both use yield to add one element, and yield* to splice an existing iterable or stream into the result.

CodePudding user response:

async


The Future class, part of dart:async, is used for getting the result of a computation after an asynchronous task has completed. This Future value is then used to do something after the computation finishes. Once the read operation is completed, the execution control is transferred within "then()".

async*


Async and Await keywords are used to provide a declarative way to define the asynchronous function and use their results. The async keyword is used when we want to declare a function as asynchronous and the await keyword is used only on asynchronous functions

sync*


In Dart language the synchronous data sequence means the instance of Iterable . The asynchronous data sequence means the instance of Stream . P.S. Generator functions can generate data items indefinitely until the function returns.

  • Related