How to Use the Streams in the Dart Programming Language in Ubuntu 20.04?
To use the streams in the Dart programming language effectively, you need to understand the following two examples that have been implemented on a Ubuntu 20.04 system:
Example # 1: Creating a Dart Stream for Calculating the Sum of Multiple Integers
In this example, we will be creating a Dart stream that will keep on reading integers until they exist, and as soon as it reads a new one, it will add that to the existing ones. This process will carry on until there are no further integers left, after which you will be able to get the sum of all of these integers. You will be able to understand it clearly by going through the Dart script shown in the image below:
In this example, we have first created an integer type “Future,” i.e., it is capable of returning an integer value which is “sum” in this case. We have named this future “sumStream.” This is capable of accepting a stream of integers. Then, we have also used the “async” keyword while defining this future. This keyword is used whenever you want to use the “await for” loop within a script. Inside this future, we have created a variable named “sum” and initialized it with “0” so that it does not contain any garbage value.
Then, we have made use of the “await for” loop. This loop will keep running for as long as there are more elements within our integer stream. This loop basically iterates over the integer stream that has been passed to our future. Inside this loop, we are calculating the sum of all the values of the integer stream. This loop will break when there are no further elements left within the stream, after which this future will return the “sum” to the “main()” function.
Then, we have declared the “main()” function having the “Future<void>” return type and have also used the “async” keyword with its declaration. After that, we have declared and initialized a stream of four different integers within our driver function. Then, we have created a “sum” variable for holding the result of the “sumStream” future. We have called this future with the “await” keyword while passing the integer stream to it. Finally, we have used a “print” statement for printing the result of this script, i.e., the sum of the integer stream on the terminal.
For the execution of this Dart script, we have utilized the subsequent command:
The output of our Dart script, i.e., the sum of our integer stream, is shown in the image below:
Example # 2: Creating a Dart Stream for Calculating the Product of Multiple Integers
In this example, we want to create a Dart stream for calculating the product of multiple integers. This example is pretty much similar to our first example in terms of the usage of the Dart streams; however, its main logic, i.e., the calculation of the product, will differ from the first example. The following Dart script explains this functionality:
In our second example, we have declared a Dart future with the integer return type. We have named this future “productStream,” and this future will take an integer stream as input. Again, we have used the “async” keyword since we will be using the “await for” loop in our future. Inside this future, we have declared a variable named “product” and have assigned to it the value “1”. This time, we have not initialized this variable with “0” as we did in the case of the sum. It is so because multiplying any number with “0” reduces the whole output to “0”. This is exactly why we need to keep the value of this variable “1” if we wish to multiply the elements of the integer stream.
Then, we have used an “await for” loop that iterates over the integer stream that has been passed to this future. Inside this loop, we are calculating the product of all the elements of our integer stream. These elements are read one by one, and the value of the “product” variable is updated. Finally, when all the elements of the integer stream have been read, this future will return the product of all these integers to the “main()” function.
Then, in our “main()” function, we have declared a stream of three integers. After that, we have created a “product” variable for holding the result of our “productStream” future. Now, we have printed the result of the “product” variable on the terminal.
The image shown below displays the output of our “productStream” future, i.e., the product of all the elements of our integer stream.
This article was designed to share with you a relatively new concept associated with the Dart programming language, i.e., Dart streams. To make this concept understandable for you, we have implemented two simple examples in Ubuntu 20.04 and have explained them both in-depth. After going through these two examples, you will be able to grasp the concept of streams in the Dart programming language very well.