In my Flutter app, I have a stream created using the Stream.periodic constructor (the timeleft, minutes, and seconds variables are defined in the surrounding context):
_countdown = Stream.periodic(
Duration(seconds: timeLeft),
(count) {
var value = '$minutes : $seconds';
if (seconds-- == 0) {
seconds = 59;
--minutes;
}
return value;
},
);
Then I have a StreamBuilder that consumes the stream:
StreamBuilder<String>(
stream: _countdown,
builder: (context, snapshot) {
String message;
if (snapshot.connectionState == ConnectionState.done) {
message = 'Finito!';
} else {
message = snapshot.data;
}
if(snapshot.hasError)
message = '${snapshot.error}';
return Text(
message ?? 'HOLD',
style: DefaultTextStyle.of(context)
.style
.apply(fontSizeFactor: 3.0),
);
},
),
When I run my app, I only see HOLD on the screen, like the snapshot.data variable is null. When I debug my app, the function passed to the Stream.periodic constructor is never called. I have also verified that the timeleft variable is greater than 0. It is actually 360, so the stream should be sending a new value every second and displaying it.
Why are there no values coming through the stream?
EDIT: I accepted diegodeveloper's answer since that was correct. If you would like to see how I ended up implementing the timer with RxDart check my answer below.