Generator Functions - Flutter in Focus

Flutter · 155K views · 2019 · 6:54

Open on YouTube

Python, Ruby & scripting languagesProgramming language designSoftware practice, open source & careerstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about async vs async*, yield vs yield*, and when learning Dart futures and streams from a structured series.

Recommended by 3 people across 3 different months, 2020 to 2023.

What it is

A roughly seven minute episode of the official Flutter in Focus series on asynchronous Dart, produced by the Flutter team. It explains sync* and async* generator functions, how yield and yield* work, and where generators are useful, including a recursive example.

What people said

Recommends Suragch · 2020-02 · Stack Overflow · on “What's the difference between async and async* in Dart?”

Short answer async gives you a Future async* gives you a Stream . async You add the async keyword to a function that does some work that might take a long time. It returns the result wrapped in a Future . Future<int> doSomeLongTask() async { await Future.delayed(const Duration(seconds: 1)); return 42; } You can get that result by awaiting the Future: main() async { int result = await doSomeLongTask(); print(result); // prints '42' after waiting 1 second } async* You add the async* keyword to make a function that returns a bunch of future values one at a time. The results are wrapped in a Stream. Stream<int> countForOneMinute() async* { for (int i = 1; i <= 60; i++) { await Future.delayed(con

Recommends Suragch · 2020-02 · Stack Overflow · on “Difference between yield and yield* in Dart”

Short answer yield provides a single value in an Iterable or a Stream. yield* provides multiple values from another Iterable or Stream. Iterable (synchronous) example Take a look at how the following two iterables interact: void main() { final myIterable = getValues(); for (int value in myIterable) { print(value); } } Iterable<int> getValues() sync* { yield 42; yield* getThree(); yield* [6, 7, 8]; yield 24; } Iterable<int> getThree() sync* { yield 1; yield 2; yield 3; } Here's what's happening: sync* creates an iterable, making it a synchronous generator function [LINK] . yield 42 provides a single value (42) for the getValues generator function. yield* provides multiple values for getValues

Recommends Tirth Patel · 2020-08 · Stack Overflow · on “what is the difference between async and async* in dart?”

Checkout 4th [LINK] & 5th [this video] of the "Flutter in Focus" series by Flutter team. They've explained them pretty well.

Recommends bitinerant · 2023-02 · Stack Overflow · on “What is a Future and how do I use it?”

If you haven't dealt with futures in another programming language, it will take some time to grasp the concepts. I found Google's Flutter in Focus video series invaluable: ☐1. Isolates and Event Loops [LINK] ; ☐2. Futures [LINK] ; ☐3. Streams [LINK] ; ☐4. Async/Await [LINK] ; ☐5. Generators [this video]

Cites abann sunny · 2021-11 · Stack Overflow · on “Difference between yield and yield* in Dart”

I have created a dart pad [LINK] link to help people experiment: Yield* is used to yield a whole iterable one value at a time with out using a loop. These 2 functions do exactly the same thing, generates an iterable based on the start and finish values. Iterable<int> getRangeIteration(int start, int finish) sync* { for(int i = start; i<= finish; i++){ yield i; } } Iterable<int> getRangeRecursive(int start, int finish) sync* { if (start <= finish) { yield start; yield* getRangeRecursive(start + 1, finish); } } In the first implementation (yield i) would imply type Iterable which matches the function's return type. now if in the second implementation instead of yield* getRangeRecursive(start +

Cites Georgios · 2022-01 · Stack Overflow · on “Dart Generator Functions (yield / yield*)”

I am trying to understand how the generator functions work in dart/flutter. There is a video from the flutter team Generator Functions - Flutter in Focus [this video] where they discuss the following example. Iterable<int> getRange(int start, int finish) sync* { if (start <= finish) { yield start; print('start: $start'); for (final val in getRange(start+1, finish)) { yield val; print('val: $val'); } } } only without the print lines. I went and called that function in a main like this void main() async { getRange(0, 3).forEach(print); } and got the following results: 0 start: 0 1 val: 1 start: 1 2 val: 2 val: 2 start: 2 3 val: 3 val: 3 val: 3 start: 3 The part from where I do not understand the ou

Cites Suragch · 2023-05 · Stack Overflow · on “Difference between yield and yield* in Dart”

@Hackmodford, please explain more. I pulled the recursive part from the Flutter team video [this video] . I still don't understand it well though, especially if it isn't about recursion.