在一个StreamBuilder中使用2个不同的流 [英] Use 2 different streams in one StreamBuilder

查看:54
本文介绍了在一个StreamBuilder中使用2个不同的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能在一个Stream Builder中使用2个不同的流吗?或者我应该如何解决这个问题?

Use 2 different streams in one Stream Builder is this possible or how should I tackle this issue?

我尝试对两者都使用一个流,但是主题数据的BehaviorSubject是ThemeData模型,而languageStream是String,并且无法正常工作.

I tried using one stream for both of them but the BehaviorSubject for theme data is ThemeData model and for the languageStream a String and it is not working out.

child: StreamBuilder<String>(
        stream: translationsBloc.languageOutStream,
//      stream: themeBloc.outFirestore,

这些是我要使用的流

集团看起来像这样:

 final _themeSubject = BehaviorSubject<ThemeData>();
  Stream<ThemeData> get outTheme => _themeSubject.stream;

和另一个这样的人

BehaviorSubject<String> _languageController = BehaviorSubject<String>();
  Stream<String> get languageOutStream => _languageController;

我需要两个流都位于主页中,因为这会影响整个应用程序.

I need both streams to be in the main page as it affects the entire app.

推荐答案

我猜最好的方法是嵌套 StreamBuilders .这并不罕见,实际上经常使用.

I guess the best approach would be nested StreamBuilders. That is not uncommon and is actually used pretty often.

当然,只有第一个流为true时,才可以加载内部流,但这取决于您的应用程序.如果您提供更多代码,我们将为您提供帮助.但是基本结构可能会变成这样:

Of course you might load the inner stream only once snapshot.data is true for the first stream but that depends on your application. If you provide more code I'll help you with that. But the basic structure might end up like this:

Widget build(BuildContext context) {
  return StreamBuilder<String>(
      stream: translationsBloc.languageOutStream,
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        return StreamBuilder<ThemeData>(
            stream: themeBloc.outFirestore,
            builder: (BuildContext context, AsyncSnapshot<ThemeData> snapshot) {
              return Container();
            });
      });
}

这篇关于在一个StreamBuilder中使用2个不同的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆