We will create a very simple countdown app, which goes from 60 to 0, using a BLoC:
- Create a new file in the lib folder of your project and call it countdown_bloc.dart.
- In the countdown_bloc.dart file, import the dart:async library:
import 'dart:async';
- Still in the countdown_bloc.dart file, create a new class, called TimerBLoC:
class TimerBLoC {}
- At the top of the TimerBLoC class, declare an integer, StreamController, a Stream getter, and StreamSink:
int seconds = 60;
StreamController _secondsStreamController = StreamController();
Stream get secondsStream =>
_secondsStreamController.stream.asBroadcastStream();
StreamSink get secondsSink => _secondsStreamController.sink;
- In the TimerBLoC class, create an async method called decreaseSeconds, which decreases the seconds and adds the new value to the sink:
Future decreaseSeconds() async {
await Future.delayed(const Duration(seconds: 1));
seconds...