For this recipe, we will show a random number on the screen, leveraging StreamControllers, and their sink property:
- In the stream.dart file, import the dart:async library:
import 'dart:async';
- At the bottom of the stream.dart file, add a new class, called NumberStream:
class NumberStream {
}
- In the NumberStream class, add a stream controller of the int type, called controller:
StreamController<int> controller = StreamController<int>();
- Still in the NumberStream class, add a method called addNumberToSink, with the code shown here:
addNumberToSink(int newNumber) {
controller.sink.add(newNumber);
}
- Next, add a close method at the bottom of the class:
close() {
controller.close();
}
- In the main.dart file, add the imports to dart:async and dart:math:
import 'dart:async';
import 'dart:math';
- At the top of the _StreamHomePageState class, declare three variables: int, StreamController...