简体   繁体   English

如何在 flutter 中拥有多个相同数据类型的 stream 提供程序?

[英]How to have multiple stream providers of the same datatype in flutter?

I want to have two stream providers of type QuerySnapshot from two different Firebase collections.我想从两个不同的 Firebase collections 中获得两个 QuerySnapshot 类型的 stream 提供程序。 When I tried just making two stream providers of the same value, one of the stream providers just overrided the other one.当我尝试只制作两个具有相同值的 stream 提供程序时,其中一个 stream 提供程序只是覆盖了另一个。 Is it possible to somehow differentiate between two stream providers of the same data type?是否有可能以某种方式区分相同数据类型的两个 stream 提供程序? Here is my current code for the two providers:这是我当前为这两个提供商提供的代码:

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        StreamProvider.value(value: FirestoreHelper.getClassCollectionReference(context).snapshots()),
        StreamProvider.value(value: FirestoreHelper.getTaskCollectionReference(context).snapshots()),
      ],
      

Yes, there is always a way.是的,总是有办法的。 What you can do is make a custom class or model class for any of your Query and then map the snapshot coming from that query with the custom class. What you can do is make a custom class or model class for any of your Query and then map the snapshot coming from that query with the custom class.

class MySnap {
  final QuerySnapshot snapshot;
  MySnap(this.snapshot);
}

Change any of Your provider to this将您的任何提供商更改为此

StreamProvider.value(value:FirestoreHelper.getClassCollectionReference(context).snapshots().map<MySnap>((snap) => MySnap(snap)),

For accessing the data use:访问数据使用:

var mysnap = Provider.of<MySnap>(context);
var data =  mysnap.snapshot;///This is your querysnapshot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM