Flutter BLoC:使用嵌套 StreamBuilder 是一种不好的做法吗?

Ali*_*min 5 architecture flutter bloc

是否有更好的方法将小部件公开给来自不同 BLoC 的两个或多个流?到目前为止,我一直在使用nested 来StreamBuilder监听尽可能多的流,就像下面粘贴的代码一样。这是一个好的做法吗?

StreamBuilder(
    stream: firstBloc.stream1,
    builder: (_, AsyncSnapshot snapshot1) {
        return StreamBuilder(
            stream: secondBloc.stream2,
            builder: (_, AsyncSnapshot snapshot2) {
                return CustomWidget(snapshot1.data, snapshot2.data);
            }
        )
    }
)

Run Code Online (Sandbox Code Playgroud)

使用rxdart像这样的运算符combineLatest2感觉很笨重,因为大多数时候我不希望一个块被用来了解另一个块中的流。

Rém*_*let 3

您不能使用小部件做其他事情。这是小部件系统的局限性之一:事情往往会变得非常嵌套

不过有一个解决方案:Hooks,一项来自 React 的新功能,通过flutter_hooks移植到 Flutter (我是维护者)。

最终结果变成这样:

final snapshot1 = useStream(firstBloc.stream1);
final snapshot2 = useStream(secondBloc.stream2);

return CustomWidget(snapshot1.data, snapshot2.data);
Run Code Online (Sandbox Code Playgroud)

这与两个嵌套的行为完全一样StreamBuilder,但是一切都是在没有嵌套的情况下在同一个内完成的。