Streams #1
This lesson introduces Dart Streams. You will learn to create a Stream of numbers using different approaches.
We'll cover the following
Streams
A Stream is a sequence of asynchronous events. Streams are useful in providing an asynchronous sequence of data.
Let’s review some key terms before we learn how to create a Stream.
Key terms
Stream: A Stream is a sequence of asynchronous events. It lets you know when the next event is ready rather than asking for it.
Data Sequence: The data sequence is made up of either user-generated events or data read from files.
Async: Functions with the await
keyword need to be marked as async
. Functions marked with async
keywords do not suspend immediately but execute synchronously until the first await
or return
is called.
Creating a Stream
You will learn to generate a Stream of events that consists of numbers using two different methods.
Using a generator function
One way to create a Stream is by using a generator function. It helps to produce a sequence of values in a lazy manner.
In this example, we will create a Stream using a for
loop and the yield
keyword.
The yield
keyword is helpful in delivering values. The async*
is an asynchronous generator that returns a Stream object. You will learn more about generators later in this course.
The main()
method is the entry point to the program that executes createStreamUsingGenerators()
to create and print it’s contents.
Get hands-on with 1400+ tech skills courses.