StreamBuilder is not rebuilding on new event in stream
Your bloc needs to be a type of Stream. The same stream type as your StreamBuilder. For example your bloc needs to be a Stream<Alarm>
. Otherwise stream: widget.bloc.alarmController.stream,
will only be called once and won't act as an asynchronous stream of data.
Your Streambuilder needs to check for connection states
Widget build(BuildContext context) { print("rebuilding..."); // as of now this gets called only on view initialization and never again - i.e. not on new events going through alarmController.stream return StreamBuilder<Alarm>( stream: widget.bloc.alarmController.stream, initialData: Alarm(''), builder: (BuildContext context, AsyncSnapshot<Alarm> snapshot) { if (snapshot.hasError) return new Text('Error: ${snapshot.error}'); switch (snapshot.connectionState) { case ConnectionState.waiting: return Text('Loading...'); default: if (!snapshot.hasData) { return Center( child: Text(StringLiterals.NO_ALARM_DATA_MSG)) ); } return Switch( activeColor: Colors.red, value: snapshot.data.status == 'Started', onChanged: (bool _value) { _newAlarmValue = _value; _askAlarmConfirmation(); })); }
Here are the other types of connection states you can check for:
async.dart
enum ConnectionState { /// Not currently connected to any asynchronous computation. /// /// For example, a [FutureBuilder] whose [FutureBuilder.future] is null. none, /// Connected to an asynchronous computation and awaiting interaction. waiting, /// Connected to an active asynchronous computation. /// /// For example, a [Stream] that has returned at least one value, but is not /// yet done. active, /// Connected to a terminated asynchronous computation. done,}
The issue was I was instantiaing my BLOC incorrectly(second one) and working on a second parallel stream and therefore not the one passed to StreamBuilder.