我的 StreamBuilder 视图:
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(
stream: widget.bloc.alarmController.stream,
initialData: Alarm(''),
builder: (BuildContext context, AsyncSnapshot<Alarm> snapshot) {
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();
}));
});
}
我的肉块:
AlarmBloc(this.Api) {
getAlarm();
}
getAlarm() async {
Alarm response = await Api.getAlarmStatus();
alarmController.sink.add(response); // here im adding new event, therefore streambuilder should rebuild, right?
}
最后是我调用来启动新事件的代码(在这种情况下是 firebase 消息):
if(_message.notification.body.contains("Alarm") && IS_LOGGED_IN == true) {
alarmBloc.getAlarm();
}
所以问题是当新事件通过alarmController.stream时StreamBuilder不会重建。可能是什么原因?