DEV Community

Keval Domadia
Keval Domadia

Posted on

Difference between yield and await in Dart / Flutter

In summary:
yeild is used under the context of a Business Logic Component or BLoC and await is used under the context of a Future.

The yield keyword is used in a Dart generator function to return a value from the function and pause the function's execution until the next value is requested. In a Bloc, the yield keyword is used to emit a new state from the mapEventToState function in response to an event.

mapEventToState is a method in a Bloc class that maps an incoming event to a new state. It takes an event as an input and returns a state as an output. In a Bloc, the mapEventToState method is responsible for converting incoming events into new states, which can be emitted using the yield keyword. For example, in the code mapEventToState(event), the mapEventToState method is called with the event object as an argument, and it returns a new state based on the type of the event. The new state can then be emitted using the yield keyword, and it will be passed to the BlocBuilder widget to update the UI.

If you have any questions, you know where to find me.

Get good. Be good. Code hard! Cheers!

Top comments (0)