【发布时间】:2020-03-16 11:40:32
【问题描述】:
我有一个小部件isLive,它根据 bloc 返回的值更改状态。但是每次我运行应用程序时,我都会得到
在 null 上调用了 getter 'progressStateStream'
我试过关注这个answer
Widget isLive() {
return Container(
child: StreamBuilder<bool>(
stream: _bloc.progressStateStream,
builder: (context, snapshot) {
return Visibility(
maintainState: true,
visible: snapshot.data ?? false,
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(10)),
child: Container(
color: Colors.pink[50],
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text("yaay i'm visible"),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Text(
"hide"
),
color: Colors.white,
onPressed: () {
_bloc.changeProgressState(state: false);
},
)
],
),
),
),
);
},
));
}
这是我的集团
//this Subject allows sending data, error and done events to the listener
final PublishSubject<bool> _progressStateSubject = new PublishSubject();
//the listener are streaming on changes
Observable<bool> get progressStateStream => _progressStateSubject.stream;
//to change your progress state
void changeProgressState({bool state}) => _progressStateSubject.sink.add(state);
另外,如果我想用 hydrad bloc 保存状态,我会怎么做呢
【问题讨论】:
-
你的初始化状态在哪里?
-
我在初始化状态下没有初始化它
-
该消息意味着
_bloc为空。所以请告诉我你在哪里输入_bloc的实例。 -
设法通过将我的 bloc 初始化为 init 状态来解决它,谢谢
_bloc = RBloc(); _bloc.progressStateStream; -
请用新答案更新您的问题解决方案。
标签: flutter flutter-layout bloc