Streams(Flutter- Async Programming with Dart) — Part 4

Abdur Mohammed
3 min readSep 13, 2022

--

Part of Flutter/Dart Shorts: To the point| Simple.

1.0 Pre-requisites

a. Asynchronous coding experience in any language (C#, Java etc.)

b. Future (Dart)- Part 3 of this series.

c. Event loops (Dart) — Part 2 of this series.

2.0 Why is this important?

Part of the core concepts of Reactive programming in Dart.

3.0 What is a stream?

a. They’re just like Future (return value or error asynchronously) except that they can return zero or more values and errors over time.

b. Just like future, there’s a callback when the data is ready, when there’s an error and when the stream completes

4.0 Streams and Event loops

a. Event loop is doing the hard work behind the scenes.

b. It processes the data as it arrives from the stream.

5.0 Listening to Streams

a. Listen (method) can be used to subscribe to a stream.

b. It accepts a function that gets called every time the stream emits a new value.

Example:
final listener = sampleStream.listen(myFunc);

5.1 Single Subscription

a. Out of the box, streams only accept single subscriptions.

b. Throws an Error when there is more than one subscriber to a stream.

5.2 What happens when nobody is listening to a single subscription stream?

The stream holds on to its value until someone subscribes.

5.3 Broadcast Streams

Same as a single subscription but can have multiple listeners.

Example: 
final myStream = customStream()
.stream .asBroadcastStream;

5.4 Nobody is listening to a Broadcast stream?

The value gets thrown out of memory.

5.5 OnError

a. Just like a future, there’s a possibility of getting an error back instead of value.

b. To handle such scenarios, you can pass on a call back that gets called when the stream returns an error.

Example:final listener = sampleStream.listen(myFunc,
onError: logError);

5.6 cancelOnError Property

a. Can be set to false to keep listening even after an error.

b. True by default.

final listener = sampleStream.listen(myFunc,
onError: logError,
cancelOnError: false);

5.7 OnDone

Call back we can provide for when the stream finishes sending data.

final listener = sampleStream.listen(myFunc,
onError: logError,
cancelOnError: false,
onDone: notifyOnDone);

5.8 The listener/subscriber object

a. This one has methods of its own that we can call for controlling the way we want to listen to the streams.

listener.pause();listener.resume();listener.cancel();

b. The operations do what the name their names suggests.

6.0 Manipulating Streams

6.1 Map method

a. Map method can be used to take each value from the stream and convert it on the go into something else.

rubberCreator().stream
.map(convertToTyres)
.listen(myFunc);

b. Takes a function and returns a new stream typed to match the return value of the function.

6.2 Where method

a. The where method can be called on the stream to filter the results.
b. Taken in function which returns a boolean as a result which is used to filter the values.

rubberCreator().stream
.where((rubber)=> rubber.quality == 'high quality' )
.listen(myFunc);

6.3 The Distinct Method

As the name suggests, removes any duplicates from the stream.

rubberCreator().stream
.distinct()
.listen(myFunc);

There are lot more methods available that we can use to mould and change the streams as per our needs ;-)

6.4 The Async library

a. Can be used to do advanced stuff such as

  • merge two streams
  • cache results etc.

b. More about it here.

7.0 Creating your own Streams

a. Stream controllers can be used to create a new stream.

b. It gives access to both sides of the stream i.e the publisher end and the subscriber end.

Examplefinal _ controller = StreamController<rubber>();///Subscriber end:
Stream<rubber> get stream => _controller.stream;

///Publisher end:
class RubberCreator {RubberCreator(){Timer.periodic(Duration(seconds: 3}, (t) {_controller.sink.add(getNewRubber());});

c. The StreamController uses sink to publish.

d. The stream property of StreamController can be used to by other objects that subscribe to it.

That's all for this one. Next part StreamBuilders :-)

You can find me on
LinkedInhttps://www.linkedin.com/in/knowabdur

Twitter: — https://twitter.com/AbdurDeveloper

--

--

Abdur Mohammed

React Native| Flutter | Senior Software Engineer | Sydney, Australia