Updated 25 April 2023
Streams In Flutter are like pipes. If you put a value on one end and have a listener on the other end, that listener will pick it up. A stream can have multiple listeners, all of which get the same value when they enter the pipeline. Use a StreamController to put values into a stream.Streams provide an asynchronous sequence of data.
You may also check our Flutter app development services.
Type of Streams : There are two type of streams.
1. Single Subscription Stream:
A single subscription stream works great when you’re only using certain streams on a screen. You can only listen to one subscription stream once. It won’t start generating events until there is a listener, and will stop sending events when the listener stops listening, even if the source of the event can provide more data.
A single subscription stream is useful for file downloads or single operations. For example, a widget can subscribe to a stream to get value updates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import 'dart:convert'; import 'dart:async'; // Initializing a stream controller StreamController<String> controller = StreamController<String>(); // Creating a new stream through the controller Stream<String> stream = controller.stream; void main() { // Setting up a subscriber to listen for any events sent on the stream StreamSubscription<String> subscriber = stream.listen((String data) { print(data); }, onError: (error) { print(error); }, onDone: () { print('Stream closed!'); }); // Adding a data event to the stream with the controller controller.sink.add('Hello!'); // Adding an error event to the stream with the controller controller.addError('Hi!'); // Closing the stream with the controller controller.close(); } |
2. Broadcast streams:
If you need multiple parts of your app to access the same stream, use broadcast streams instead. A broadcast stream can have any number of listeners. Occurswhen an event is ready, with or without listeners. To create a broadcast stream, simply call asBroadcastStream() on an existing single subscription stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import 'dart:convert'; import 'dart:async'; // Initializing a stream controller for a broadcast stream StreamController<String> controller = StreamController<String>.broadcast(); // Creating a new broadcast stream through the controller Stream<String> stream = controller.stream; void main() { // Setting up a subscriber to listen for any events sent on the stream StreamSubscription<String> subscriber1 = stream.listen((String data) { print('Subscriber1: ${data}'); }, onError: (error) { print('Subscriber1: ${error}'); }, onDone: () { print('Subscriber1: Stream closed'); }); // Setting up another subscriber to listen for any events sent on the stream StreamSubscription<String> subscriber2 = stream.listen((String data) { print('Subscriber2: ${data}'); }, onError: (error) { print('Subscriber2: ${error}'); }, onDone: () { print('Subscriber2: Stream closed'); }); // Adding a data event to the stream with the controller controller.sink.add('Hello!'); // Adding an error event to the stream with the controller controller.addError('Hi!'); // Closing the stream with the controller controller.close(); } |
I hope you get the rough idea to how to use Stream in Flutter. For reference you can also check here.
To read more Mobikul blogs, Please click here.
Thanks for reading…
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.