我有一个具有public
流的小部件。在另一个小部件中,我想根据流的数据更新UI。因此,我像往常一样用StreamBuilder
包裹流。
在这种情况下,StreamBuilder
不会更新,但是我可以看到print
的输出。
我的代码看起来像这样(原始代码更复杂):
class WidgetA extends StatefulWidget {
final _WidgetAState _state = _WidgetAState();
Stream<String> get stream => _state?._subject?.stream;
@override
_WidgetAState createState() => _state;
}
class _WidgetAState extends State<WidgetA> {
final BehaviorSubject<String> _subject = BehaviorSubject<String>();
@override
Widget build(BuildContext context) {
return Container(
child: FlatButton(
child: const Text('click'),
onPressed: () => _subject.add(DateTime.now().toString()),
),
);
}
@override
void dispose() {
_subject.close();
super.dispose();
}
}
class WidgetB extends StatelessWidget {
@override
Widget build(BuildContext context) {
final WidgetA widgetA = WidgetA();
widgetA.stream.listen((value) => print('value: $value'));
return Column(
children: <Widget>[
widgetA,
StreamBuilder<Object>(
stream: widgetA.stream,
builder: (_, snapshot) => Text('data: ${snapshot.data}'),
),
],
);
}
}
当我将stream
与另一个stream
包裹在一起时,它会起作用。
class WidgetB extends StatefulWidget {
@override
_WidgetBState createState() => _WidgetBState();
}
class _WidgetBState extends State<WidgetB> {
final WidgetBBloc bloc = WidgetBBloc();
@override
Widget build(BuildContext context) {
final WidgetA widgetA = WidgetA();
bloc.init(stream: widgetA.stream);
bloc.stream.listen((value) => print('value: $value'));
return Column(
children: <Widget>[
widgetA,
StreamBuilder<Object>(
stream: bloc.stream,
builder: (_, snapshot) => Text('data: ${snapshot.data}'),
),
],
);
}
}
class WidgetBBloc {
Stream<String> get stream => _subject.stream;
final BehaviorSubject<String> _subject = BehaviorSubject<String>();
void init({@required Stream<String> stream}) {
assert(stream != null);
stream.listen((String s) => _subject.add(s));
}
void dispose() {
_subject.close();
}
}
为什么第一个版本的StreamBuilder
没有更新?