是否有一个StreamBuilder允许我收听多个流? 像这样:
body: MultiStreamBuilder(
streams: [bloc.state.stream, bloc.kind.stream],
builder: (context) {
我不需要Flutter StreamBuilder中的快照,因为我可以从团体中读取
答案 0 :(得分:1)
只要其中一个流得到更新,这就会更新UI(调用构建)。我不使用snapshot.data,因为我直接从bloc读取数据,并且snapshot.data仅包含布尔值,而不包含流的实际数据。
class _RemoteDebuggingScreenState extends State<RemoteDebuggingScreen> {
@override
Widget build(BuildContext context) {
RemoteDebugBlog bloc = BlocProvider.of(context).remoteDebugBloc;
return Scaffold(
body: StreamBuilder(
stream: Observable.combineLatest2(bloc.state.stream, bloc.kind.stream,
(b1, b2) => b1 != null || b2 != null),
builder: (context, snapshot) {
if (!snapshot.hasData) return Container();
return Column(...
有关combineLatest
的一些精美图片的更多信息,请查看以下内容:
https://www.burkharts.net/apps/blog/rxdart-magical-transformations-of-streams/
现在了解了以上内容,我将其移至了一个小部件中:
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
class DoubleStreamBuilder extends StatelessWidget {
@override
DoubleStreamBuilder(
{@required this.stream1, @required this.stream2, @required this.builder});
final Stream stream1;
final Stream stream2;
final Widget Function(BuildContext) builder;
Widget build(BuildContext context) => StreamBuilder(
stream: Observable.combineLatest2(
stream1, stream2, (b1, b2) => b1 != null || b2 != null),
builder: (context, snapshot) => builder(context));
}
答案 1 :(得分:-1)
是的。
https://pub.dev/packages/multiple_stream_builder
// Pass multiple streams into a single StreamBuilder
Widget build(BuildContext context) {
return StreamBuilder3<int, int, int>(
streams: Tuple3(stream1, stream2, stream3),
initialData: Tuple3(0, 0, 0),
builder: (context, snapshots) {
return Text(
'stream1: ${snapshots.item1.data} - stream2: ${snapshots.item2.data} - stream3: ${snapshots.item3.data}',
);
},
);
}