A stream is one of the challenging topics for the beginner. It took lots of efforts to understand. In order to understand the Streams, you will need to go through the various Examples and then you will get what Exactly Streams is.

At the end of the post, I will write one Flutter application which will be based on the same example which I had discussed in Dart.

I am also going to put my understanding of Streams into the words and Examples.

Streams are a concept Dart. So, I will start streams in Dart and end with applying those concepts in Flutter.

Asynchronous programming in Dart is characterized by the Future and Stream classes.

What is Future?

A Future is used to represent a potential value, or error, that will be available at some time in the future.

A simple Example of Future with potential value.

// asynchronous data

main() async {

String x = await HelloAsync();

print(x);

} Future<String> HelloAsync() async{

await Future.delayed(Duration(seconds:5));

return 'Message from Future.';

}

Why is Streams?

Streams provide an asynchronous sequence of data.

How stream and Future are similar?

Both works asynchronously

Both have some potential value

How stream and Future are different?

A stream is a combination of Futures

Future has only one response but Stream could have any number of Response.

If you had ever sent any request to any API in your flutter application then probably you know about async ( Future ).

There are two kinds of streams :

Single Subscription: There could be a maximum of one listener to this stream. Broadcast: There could be the infinite number of the listener to this stream.

All the example which I will discuss in this post will be Single Subscription based. Once you understand the single subscription then you can easily understand the Broadcast.