dart - StreamBuilder 不会在流中的新事件上重建
问题描述
我的 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不会重建。可能是什么原因?
解决方案
您的集团需要是一种流。与您的 StreamBuilder 相同的流类型。例如,您的集团需要是Stream<Alarm>
. 否则stream: widget.bloc.alarmController.stream,
只会被调用一次,并且不会充当异步数据流。
您的 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<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();
}));
}
以下是您可以检查的其他类型的连接状态:
异步飞镖
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,
}
推荐阅读
- homebrew - 发生冲突时自动进行 brew 升级
- javascript - 该数组不适用于我的图片输出
- javascript - React Redux - 布尔值未定义
- neo4j - Neo4j:Cypher:如何按模式删除标签?
- matlab - 带有 parfor 循环的 GPU 上的 MATLAB arrayfun
- java - Libgdx,在渲染对象层中实现单位比例
- c# - 如何将 Relational() 重构为 NetCore3.0
- swiftui - SwiftUI 等效于 UITextField.inputAccessoryView
- c++ - “const 引用”实际上不应该被称为“对 const 的引用”吗?
- ios - 如何将枚举类型的参数传递给 .h 和 .m 文件中的方法