Dart – Streams

Simple stream examples:

Dart
Dart

A stream which continuously sent random values:

Dart
  • The function return a Stream. That means we can to subscribe to the stream.
  • The async* means run the function asynchronously. Execution will continue even after “returning” a value.
  •  yield is a return function which doesn’t exit the function. Instead it continues executing the rest of the code after yield.

Stream examples with a StreamTransformer:

A Stream Transformer allows us to perform data transformations on a Stream. These transformations are then pushed back into the Stream to be received by all the listeners defined for that particular stream.

Example 1:

Dart

This transformer is made as the type StreamTransformer<String, String> which means it takes a String and convert it into a String. But because map cannot determine the return type of value it most assume that value are dynamic. So map returns a Stream<dynamic> which are not compatible with the transformer.

We can tell map what type it is going to return on runtime by writing .map<String>((value) => value). But in general we should not throw away the generic part of types.

Example 2:

Dart
Dart

https://medium.com/flutter-community/flutter-stream-basics-for-beginners-eda23e44e32f