gpt4 book ai didi

flutter - 是否有 Flutter StreamBuilder 允许我收听多个流?

转载 作者:行者123 更新时间:2023-12-03 02:55:55 25 4
gpt4 key购买 nike

是否有 StreamBuilder 允许我收听多个流?
就像是:

   body: MultiStreamBuilder(
streams: [bloc.state.stream, bloc.kind.stream],
builder: (context) {

我不需要像 Flutter StreamBuilder 那样的快照,因为我可以从 bloc 中读取

最佳答案

每当其中一个流获得更新时,就会更新 UI(调用构建)。我不使用 snapshot.data 因为我直接从 bloc 读取数据,而 snapshot.data 只包含一个 bool 而不是流的真实数据。

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: Rx.combineLatest2(
stream1, stream2, (b1, b2) => b1 != null || b2 != null),
builder: (context, snapshot) => builder(context));
}


用法现在清晰而简单,如下所示:
 return DoubleStreamBuilder(
stream1: settingsBloc.uiVin.stream,
stream2: settingsBloc.isPseudoVin.stream,
builder: (context) {
=> this updates when stream 1 or 2 have new values
}

关于flutter - 是否有 Flutter StreamBuilder 允许我收听多个流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58552272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com