Flutter 将要在单个StreamBuilder中使用的流与rxdart组合
几天来,我一直试图找到这个问题的答案。 许多例子让我感到困惑,我真的不明白如何使用rxdart实现它们,尽管它们看起来很简单 我正在尝试将Firestore中的4个集合合并到一个StreamBuilder中 在build方法中,我将返回一个StreamBuilder,如下所示:Flutter 将要在单个StreamBuilder中使用的流与rxdart组合,flutter,dart,google-cloud-firestore,Flutter,Dart,Google Cloud Firestore,几天来,我一直试图找到这个问题的答案。 许多例子让我感到困惑,我真的不明白如何使用rxdart实现它们,尽管它们看起来很简单 我正在尝试将Firestore中的4个集合合并到一个StreamBuilder中 在build方法中,我将返回一个StreamBuilder,如下所示: Widget build(BuildContext context) { return Scaffold( appBar: _bla(), body: SingleChildScr
Widget build(BuildContext context) {
return Scaffold(
appBar: _bla(),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
return new StreamBuilder(
stream: allList, // here is my main issue
...
),
]
),
),
);
}
类型“\u BroadcastStream”不是“Observable”类型的子类型
因此,我尝试在初始化allList时删除对asBroadcastStream()的调用,但错误是相同的。
在发布我的问题之前,我已经研究了这些例子:将这些流组合成单个流,然后在StreamBuilder中使用的正确方法是什么? 你能举个小例子吗 谢谢大家! snapshots()返回简单的dart异步流,而不是rxdart的可观察流。您只需将其包装即可从中创建可观察对象:
Observable firstList=Observable(Firestore.instance.collection(bla.snapshots());
UPD:Observable扩展了Stream类,它可以像任何常规流一样使用。
RxDart没有引入新的API(与任何其他语言的实现不同)。相反,它使用的是dart的默认异步库,它已经有了和之类的类。Observable类只是扩展了Stream类,所以您可以使用Observable实例而不是默认的dart流(例如在StreamBuilder中)。RxDart的Observable也可以用于默认流
您遇到的另一个错误是,您试图从SomeList实例访问“documents”字段,但您的类没有这样的字段。在使用此字段之前,我已尝试使用Stream,所有操作都按预期进行。然后我尝试合并多个流并找到rxdart。在这里,一切都让我感到困惑。我感到困惑:您是否得到了
“是否不是类型为“可观察”
的子类型错误?我在StreamBuilder中使用allList.asBroadcastStream()时得到了此错误。我删除了asBroadcastStream(),现在我得到了一个新的异常。那么stacktrace是什么呢?前8-10帧?似乎您正试图从SomeList实例中获取“documents”字段,该实例是您的类中没有此类字段的。
Stream<QuerySnapshot> firstList = Firestore.instance.collection(bla).snapshots();
Stream<QuerySnapshot> secondList = Firestore.instance.collection(blabla).snapshots();
Stream<QuerySnapshot> thirdList= Firestore.instance.collection(blablabla).snapshots();
Stream<QuerySnapshot> fourthList= Firestore.instance.collection(blablablabla).snapshots();
Observable<SomeList> allList = Observable.combineLatest4(
firstList,
secondList,
thirdList,
fourthList, (a, b, c, d) => SomeList(a, b, c, d));
class SomeList{
QuerySnapshot firstList;
QuerySnapshot secondList;
QuerySnapshot thirdList;
QuerySnapshot fourthList;
SomeList(this.firstList, this.secondList, this.thirdList,
this.fourthList);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: _bla(),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
return new StreamBuilder(
stream: allList, // here is my main issue
...
),
]
),
),
);
}
Class 'SomeList' has no instance getter 'documents'.
Receiver: Instance of 'SomeList'
Tried calling: documents
type '_BroadcastStream<QuerySnapshot>' is not a subtype of type 'Observable<QuerySnapshot>'