A Stream is a sequence of asynchronous events. It is like an asynchronous Iterable—where, instead of getting the next event when you ask for it, the stream tells you that there is an event when it is ready.
In other words, streams are a source of asynchronous events delivered sequentially. There are data events, which are sometimes referred to as elements of the stream due to a stream's similarity to a list, and there are error events, which are notifications of failure. Once all data elements have been emitted, a special event signaling the stream is done will notify any listeners that there is no more.
Advantage of Streams:
The primary advantage of using streams to communicate is that it keeps code loosely coupled. The owner of a stream can emit values as they become available, and it doesn't need to know anything about who's listening or why. Similarly, consumers of the data need only adhere to the stream interface, and the means by which the stream's data is generated are entirely hidden.
Points to remember:
- A stream is like a pipe, you put a value on the one end, and if there’s a listener on the other end that listener will receive that value.
- You can process a stream using either await for or listen() from the Stream API.
How Streams are created?
Streams can be created in many ways: the asynchronous await for iterates over the events of a stream like the for loop iterates over an Iterable.
Example 1:
Dart
Future<int> sumStream(Stream<int> stream) async {
var sum=0;
await for(var value in stream) {
sum += value;
}
return sum;
}
Future<void> main() async {
final stream = Stream<int>.fromIterable([1,2,3,4,5]);
final sum = await sumStream(stream);
print('Sum: $sum');
}
Output:
Explanation: This code simply receives each event of a stream of integer events, adds them up, and returns (a future of) the sum. When the loop body ends, the function is paused until the next event arrives or the stream is done.
In function, we have used the async keyword, which is required when using the await for a loop.
Important concepts of Streams in Flutter:
Stream Controller: A StreamController simplifies stream management, automatically creating a stream and sink, and providing methods for controlling a stream's behavior. A StreamController object in Dart does exactly what the name suggests, it controls Dart Streams. The object is used to create streams and send data, errors, and done events on them. Controllers also help check a stream’s properties, such as how many subscribers it has or if it’s paused.
Stream Builders: StreamBuilder is a widget that uses stream operations and basically, it rebuilds its UI when it gets the new values that are passed via Stream it listens to.
StreamBuilder requires 2 parameters:
- stream: A method that returns a stream object
- builder: widgets that will be returned during different states of a streambuilder.
Practical implementation of Stream Controller and Stream builder using Count down app:
We will create a Flutter count-down app using Stream Controller and Stream builder. In this Flutter StreamController Example, We will simply build an app that can count down a value from n to 0 using the stream Controller sink & update the UI.
example1: main.dart
Dart
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CounterApp()
);
}
}
class CounterApp extends StatefulWidget {
const CounterApp({Key? key}) : super(key: key);
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State<CounterApp> {
// create instance of streamcontroller class
StreamController _controller = StreamController();
int _counter = 10;
void StartTimer() async{
// Timer Method that runs every second
Timer.periodic(Duration(seconds: 1), (timer) {
_counter--;
// add event/data to stream controller using sink
_controller.sink.add(_counter);
// will stop Count Down Timer when _counter value is 0
if(_counter<=0){
timer.cancel();
_controller.close();
}
});
}
@override
void dispose() {
super.dispose();
// Destroy the Stream Controller when use exit the app
_controller.close();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
StreamBuilder(
initialData: _counter,
stream: _controller.stream,
builder: (context,snapshot){
return Text('${snapshot.data}');
}
),
SizedBox(
height: 20,
),
ElevatedButton(onPressed: (){
// start the timer
StartTimer();
}, child: Text('Start Count Down'))
],
),
),
);
}
}
Output:
Explanation:
There are two important points to know -
- Sink: In Flutter Streams, a Sink is a point from where we can add data into the stream pipe.
- Source: In Flutter Stream, a Source is a point from where we can keep listening to stream data or get the data that is been added into the stream.
In the above code we have created StreamController & add data to it using sink.add().
We can see below this is how we used the stream controller in the above code:
// Create Stream
StreamController _controller = StreamController();
int _counter = 60;
// add event/data to stream controller using sink
_controller.sink.add(_counter);
Here we put the initial count to 10. So from 10, it will go in reverse order till the value gets to 0.
Now we need to listen to the data coming into the stream & print the data on the screen, so for that, we used the StreamBuilder widget to listen to the asynchronous event.
Below is a pseudo-code that how we used stream builder in the above program to collect data and print on the screen -
StreamBuilder(
initialData: _counter,
stream: _controller.stream,
builder: (context,snapshot){
return Text('${snapshot.data}');
}
)
Two types of Streams:
- Single subscription streams
- Broadcast streams
1. Single Subscription Streams:
Single subscription streams are the default. They work well when you’re only using a particular stream on one screen.
A single subscription stream can only be listened to once. It doesn’t start generating events until it has a listener and it stops sending events when the listener stops listening, even if the source of events could still provide more data.
Single subscription streams are useful to download a file or for any single-use operation. For example, a widget can subscribe to a stream to receive updates about a value, like the progress of a download, and update its UI accordingly.
Example 1.
Dart
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('GeeksforGeeks!');
// Adding an error event to the stream with the controller
controller.addError('Error!');
// Closing the stream with the controller
controller.close();
}
Output:
2. Broadcast streams:
If you need multiple parts of your app to access the same stream, use a broadcast stream, instead. A broadcast stream allows any number of listeners. It fires when its events are ready, whether there are listeners or not. To create a broadcast stream, you simply call asBroadcastStream() on an existing single subscription stream.
Syntax: final broadcastStream = singleStream.asBroadcastStream();
Example 2.
Dart
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('GeeksforGeeks!');
// Adding an error event to the stream with the controller
controller.addError('Error!');
// Closing the stream with the controller
controller.close();
}
Output :
Let’s look into some of the classes’ useful methods that are used in the above programs:
- add() method: handles forwarding any data to the sink.
- addError() method: If an error occurs and your stream's listeners need to be informed then addError() is used.
- listen() method: We listen to the stream for the data incoming with .listen() method.
Similar Reads
Dart Tutorial Dart is an open-source general-purpose programming language developed by Google. It supports application development on both the client and server side. However, it is widely used for the development of Android apps, iOS apps, IoT(Internet of Things), and web applications using the Flutter Framework
7 min read
Basics
Data Types
Dart - Data TypesLike other languages (C, C++, Java), whenever a variable is created, each variable has an associated data type. In Dart language, there are the types of values that can be represented and manipulated in a programming language. In this article, we will learn about Dart Programming Language Data Types
8 min read
Basics of Numbers in DartLike other languages, Dart Programming also supports numerical values as Number objects. The number in Dart Programming is the data type that is used to hold the numeric value. Dart numbers can be classified as: int (Integer) The int data type is used to represent whole numbers.Declaring Integer in
6 min read
Strings in DartA Dart string is a sequence of UTF-16 code units. With the same rule as that of Python, you can use either single or double quotes to create a string. The string starts with the datatype String or Var : String string = "I love GeeksforGeeks";var string1 = 'GeeksforGeeks is a great platform for upgra
6 min read
Dart - SetsSets in Dart is a special case in List, where all the inputs are unique i.e. it doesn't contain any repeated input. It can also be interpreted as an unordered array with unique inputs. The set comes into play when we want to store unique values in a single variable without considering the order of t
6 min read
Dart Programming - MapIn Dart programming, Maps are dictionary-like data types that exist in key-value form (known as lock-key). There is no restriction on the type of data that goes in a map data type. Maps are very flexible and can mutate their size based on the requirements. However, it is important to note that all l
7 min read
Queues in DartDart also provides the user to manipulate a collection of data in the form of a queue. A queue is a FIFO (First In First Out) data structure where the element that is added first will be deleted first. It takes the data from one end and removes it from the other end. Queues are useful when you want
3 min read
Data Enumeration in DartEnumerated types (also known as enumerations or enums) are primarily used to define named constant values. The enum keyword is used to define an enumeration type in Dart. The use case of enumeration is to store finite data members under the same type definition. Declaring enumsenum variable_name{ //
3 min read
Control Flow
Key Functions
Dart - Anonymous FunctionsAn anonymous function in Dart is like a named function but they do not have names associated with it. An anonymous function can have zero or more parameters with optional type annotations. An anonymous function consists of self-contained blocks of code and that can be passed around in our code as a
2 min read
Dart - main() FunctionThe main() function is a predefined method in Dart. It is the most important and mandatory part of any dart program. Any dart script requires the main() method for its execution. This method acts as the entry point for any Dart application. It is responsible for executing all library functions, user
2 min read
Dart - Common Collection MethodsList, Set, and Map share common functionalities found in many collections. Some of this common functionality is defined by the Iterable class, which is implemented by both List and Set.1. isEmpty() or isNotEmptyUse isEmpty or isNotEmpty to check whether a list, set, or map has items or not.Example:D
2 min read
How to Exit a Dart Application Unconditionally?The exit() method exits the current program by terminating the running Dart VM. This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination. This is a similar exit in C/C++, Java. This method doesn't wait for any asynchronous operations to term
2 min read
Dart - Getters and SettersGetters and Setters, also called accessors and mutators, allow the program to initialize and retrieve the values of class fields respectively. Getters or accessors are defined using the get keyword.Setters or mutators are defined using the set keyword.A default getter/setter is associated with every
3 min read
Dart - Classes And ObjectsDart is an object-oriented programming language, so it supports the concept of class, object, etc. In Dart, we can define classes and objects of our own. We use the class keyword to do so. Dart supports object-oriented programming features like classes and interfaces.Let us learn about Dart Classes
4 min read
Object-Oriented Programming
Dart - this keywordthis keyword represents an implicit object pointing to the current class object. It refers to the current instance of the class in a method or constructor. The this keyword is mainly used to eliminate the ambiguity between class attributes and parameters with the same name. When the class attributes
2 min read
Dart - Static KeywordThe static keyword is used for the memory management of global data members. The static keyword can be applied to the fields and methods of a class. The static variables and methods are part of the class instead of a specific instance. The static keyword is used for a class-level variable and method
3 min read
Dart - Super and This keywordSuper Keyword in DartIn Dart, the super keyword is used to refer immediate parent class object. It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keywor
4 min read
Dart - Concept of InheritanceIn Dart, one class can inherit another class, i.e. dart can create a new class from an existing class. We make use of extend keyword to do so.Terminology: Parent Class: It is the class whose properties are inherited by the child class. It is also known as a base class or super class.Child Class: It
5 min read
Instance and class methods in DartDart provides us with the ability to create methods of our own. The methods are created to perform certain actions in class. Methods help us to remove the complexity of the program. It must be noted that methods may or may not return any value, and also, they may or may not take any parameter as inp
3 min read
Method Overriding in DartMethod overriding occurs in Dart when a child class tries to override the parent class's method. When a child class extends a parent class, it gets full access to the methods of the parent class and thus it overrides the methods of the parent class. It is achieved by re-defining the same method pres
3 min read
Getter and Setter Methods in DartGetter and Setter methods are class methods used to manipulate the data of class fields. Getter is used to read or get the data of the class field, whereas setter is used to set the data of the class field to some variable. The following diagram illustrates a Person class that includes: A private va
2 min read
Abstract Classes in DartAn abstract class in Dart is defined as a class that contains one or more abstract methods (methods without implementation). To declare an abstract class, we use the abstract keyword. It's important to note that a class declared as abstract may or may not include abstract methods. However, if a clas
4 min read
Dart - Builder ClassIn Flutter, each widget has an associated build method responsible for rendering the UI. The Flutter framework automatically provides a BuildContext parameter to the build method.Widget build ( BuildContext context )Flutter takes care that there need not be any Widget apart from the build that needs
4 min read
Concept of Callable Classes in DartDart allows the user to create a callable class which allows the instance of the class to be called as a function. To allow an instance of your Dart class to be called like a function, implement the call() method. Syntax :class class_name { ... // class content return_type call ( parameters ) { ...
4 min read
Interface in DartThe interface in the dart provides the user with the blueprint of the class, which any class should follow if it interfaces that class, i.e., if a class inherits another, it should redefine each function present inside an interfaced class in its way. They are nothing but a set of methods defined for
3 min read
Dart - extends Vs with Vs implementsAll developers working with Dart for application development using the Flutter framework regularly encounter different usages of the implements, extends, and keywords. In Dart, one class can inherit another class, i.e. , Dart can create a new class from an existing class. We make use of keywords to
4 min read
Dart - Date and TimeA DateTime object is a point in time. The time zone is either UTC or the local time zone. Accurate date-time handling is required in almost every data context. Dart has the marvelous built-in classes for date time and duration in dart:core. Key Uses of DateTime in Dart:Comparing and Calculating Date
3 min read
Using await async in DartThe async and await approaches in Dart are very similar to other languages, which makes it a comfortable topic to grasp for those who have used this pattern before. However, even if you donât have experience with asynchronous programming using async/await, you should find it easy to follow along her
4 min read
Dart Utilities
Dart Programs
Dart - Sort a ListThe List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects. The core libraries in Dart are responsible for the existence of the List class, its creation, and manipulation. Sorting of the list depends
2 min read
Dart - String toUpperCase() Function with ExamplesThe string toUpperCase() method converts all characters of the string into an uppercase letter. The string toUpperCase() function returns the string after converting all characters of the string into the uppercase letter. Syntax: Str.toUpperCase()Parameter: The string toUpperCase() function doesn't
1 min read
Dart - Convert All Characters of a String in LowercaseWith the help of the toLowerCase() method in the string will convert all the characters in a string in lowercase.Syntax: String.toLowerCase() Return: string Image Representation: Example 1: Dart// main function start void main() { // initialise a string String st = "GEEKSFORGEEKS"; // print the stri
1 min read
How to Replace a Substring of a String in Dart?To replace all the substrings of a string, we make use of the replaceAll method in Dart. This method replaces all the substrings in the given string with the desired substring. Returns a new string in which the non-overlapping substrings matching from (the ones iterated by from.allMatches(this Strin
2 min read
How to Check String is Empty or Not in Dart (Null Safety)?We can check a string is empty or not by the String Property isEmpty. If the string is empty then it returns True if the string is not empty then it returns False.Syntax: String.isEmpty Return : True or False.Image Representation: Example 1:Dart// main function start void main() { // initialise a st
1 min read
Exception Handling in DartAn exception is an error that occurs inside the program. When an exception occurs inside a program, the normal flow of the program is disrupted, and it terminates abnormally, displaying the error and exception stack as output. So, an exception must be taken care of to prevent the application from te
3 min read
Assert Statements in DartAs a programmer, it is very necessary to make an errorless code is very necessary and to find the error is very difficult in a big program. Dart provides the programmer with assert statements to check for the error. The assert statement is a useful tool to debug the code, and it uses a Boolean condi
3 min read
Fallthrough Condition in DartFall through is a type of error that occurs in various programming languages like C, C++, Java, Dart ...etc. It occurs in switch-case statements where when we forget to add break statement and in that case flow of control jumps to the next line. "If no break appears, the flow of control will fall th
3 min read
Concept of Isolates in DartDart was traditionally designed to create single-page applications. We also know that most computers, even mobile platforms, have multi-core CPUs. To take advantage of all those cores, developers traditionally use shared-memory threads running concurrently. However, shared-state concurrency is error
2 min read
Advance Concepts