Extension Methods
Learn about different extension methods used to manipulate and transform data streams offered by RxDart.
One of the most useful features of RxDart is its extension methods, which allow us to extend the functionality of streams with custom operators. Extension methods are a way to add new methods to existing streams without modifying their source code.
Commonly used extension methods
RxDart provides a wide range of extension methods for working with streams. Here are some of the most commonly used ones.
The flatMap()
method
The flatMap()
method allows us to transform each item emitted by a source stream into a new stream and then flatten the emissions from those streams into a single stream.
RangeStream(1, 5).flatMap((value) => Stream.value(value * 2)).listen((value) => print(value));
Line 1: The
RangeStream
creates a stream of numbers from1
to5
.Line 2: The
flatMap()
operator transforms each number emitted by the source stream into a new stream that emits that number multiplied by2
.Line 3: The
listen()
method subscribes to the resulting stream and prints each emitted value, which will be2
,4
,6
,8
, and10
. ... ...