本文介绍了颤振Firebase将超过10个Firebase文档放入Stream& lt; List& lt; Map& gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Flutter和Firestore,我试图将10个以上的文档放入Stream< List>中.我可以在映射QuerySnapshot的集合上使用.where子句来做到这一点.但是,十个限制是一个杀手..

With Flutter and Firestore, I am trying to get more than 10 documents into a Stream<List>. I can do this with a .where clause on a collection mapping the QuerySnapshot. However, the 10 limit is a killer.

我在我的应用程序中使用了提供程序包.因此,在使用StreamProvider在Flutter中构建流时,我可以返回

I'm using the provider package in my app. So, in building a stream in Flutter with a StreamProvider, I can return a

    整个集合中的
  1. Stream< List< Map.太贵了.这些资料集上有200多个文档,而且用户过多.需要提高效率.
  2. Stream< List< Map,使用.where从集合中返回的最大Stream List列表中的最大流列表......不会切芥末.
  3. 来自文档的流<映射,返回1个文档流,其中1个流.

我需要介于1到2之间的东西.

I need something in between 1 and 2.

我有一个多达500个文档的集合,用户将选择这500个文档的任何可能的组合进行查看.用户组装类名册以查看其用户列表.

I have a Collection with up to 500 Documents, and the user will choose any possible combination of those 500 to view. The user assembles class rosters to view their lists of users.

因此,我正在寻找一种方法来获取单个文档流,例如30个文档,然后将其编译为一个列表:但是我需要将此List< Stream< Map本身作为一个Stream,以便每个文档都有效,而且我还可以过滤此流列表并对其进行排序.我正在使用提供程序包,如果可能的话,希望与之保持一致.这是我目前卡住的地方:

So I'm looking for a way to get individual streams of, say 30 documents, and then compile them into a List: But I need this List<Stream<Map to be a Stream itself so each individual doc is live, and I can also filter and sort this list of Streams. I'm using the Provider Package, and if possible would like to stay consistent with that. Here's where I am currently stuck:

所以,我目前的努力:

  Future<Stream<List<AttendeeData>>> getStreams() async {
    List<Stream<AttendeeData>> getStreamsOutput = [];
    for (var i = 0; i < teacherRosterList.length; i++) {
      Stream thisStream = await returnTeacherRosterListStream(facility, teacherRosterList[i]);
      getStreamsOutput.add(thisStream);
    }
    return StreamZip(getStreamsOutput).asBroadcastStream();
  }

感觉就像我在下面作弊:如果将快照直接放在上面的Stream thisStream中,则会收到一个等待错误,因为如果我等待,则Stream不是未来,如果我不等待,它将移动得太快并得到空错误.

Feels like I'm cheating below: I get an await error if I put the snapshot directly in Stream thisStream above as Stream is not a future if I await, and if I don't await, it moves too fast and gets a null error.

  Future<Stream<AttendeeData>> returnTeacherRosterListStream(String thisFacility, String thisID) async {
    return facilityList.doc(thisFacility).collection('attendance').doc(thisID).snapshots().map(_teacherRosterListFromSnapshot);
    }
  }

我如何在_teacherRosterListFromSnapshot中进行映射的示例(这里没有任何问题):

Example of how I'm mapping in _teacherRosterListFromSnapshot (not having any problem here):

  AttendeeData _teacherRosterListFromSnapshot(DocumentSnapshot doc) {
    // return snapshot.docs.map((doc) {

    return AttendeeData(
      id: doc.data()['id'] ?? '',
      authorCreatedUID: doc.data()['authorCreatedUID'] ?? '',
   );
  }

我的StreamProvider逻辑和错误:

My StreamProvider Logic and the error:

return MultiProvider(
            providers: [
              StreamProvider<List<AttendeeData>>.value(
                  value: DatabaseService(
                teacherRosterList: programList,
                facility: user.claimsFacility,
              ).getStreams()),
            ]

错误:参数类型为'Future< Stream< List>>'不能分配给参数类型'Stream< List>'.

Error: The argument type 'Future<Stream<List>>' can't be assigned to the parameter type 'Stream<List>'.

AttendeeData是我的地图类名称.

AttendeeData is my Map Class name.

问题摘要:

  1. 我什至可以这样做吗?我基本上是在流式传输地图流列表....这是一回事吗?
  2. 如果可以,我该怎么办?一种.由于getStreams是Future,因此我无法将其添加到StreamProvider中.如何克服这个问题?

我可以使用StreamProvider中的另一种方法来获取数据,但是它的行为不像Stream,并且状态也没有更新.我希望将其输入Provider,因为我在那很舒服,而且我可以通过这种方式很轻松地管理状态.但是,乞g不能成为选择者.

I can get the data in using another method from StreamProvider, but it's not behaving like a Stream and the state isn't updating. i'm hoping to just get this into Provider, as I'm comfortable there, and I can manage state very easily that way. However, beggars can't be choosers.

推荐答案

我自己解决了这个问题,并且由于缺少良好的开端来完成答案,因此我举了一个例子来说明那些穷追不舍的人,他们追我来尝试学习这些问题自己的东西.我是一个初学者,所以这是一条口号:

Solved this myself, and since there is a dearth of good start to finish answers, I submit my example for the poor souls who come after me trying to learn these things on their own. I'm a beginner, so this was a slog:

目标:集合中有任意数量的文档,并且您想要按文档编号提交任意数量的文档列表,并返回这些映射文档列表的单个流.您想要大于10(.where查询的存储限制),而不是所有文档...因此在QuerySnapshot和DocumentSnapshot之间.

Objective:You have any number of docs in a collection and you want to submit a list of any number of docs by their doc number and return a single stream of a list of those mapped documents. You want more than 10 (firestore limit on .where query), less than all the docs...so somewhere between a QuerySnapshot and a DocumentSnapshot.

解决方案:我们将获得QuerySnapshots的列表,我们将对其进行组合和映射,并将其作为单个流吐出.因此,我们得到了10个(最大)块,然后剩下了一些奇数.我将我的软件插入提供程序,以便可以随时随地获得它.

Solution: We're going to get a list of QuerySnapshots, we're going to combine them and map them and spit them out as a single stream. So we're getting 10each in chunks (the max) and then some odd number left over. I plug mine into a Provider so I can get it whenever and wherever I want.

因此,从我的提供程序中,我将此称为Stream值:

So from my provider I call this as the Stream value:

  Stream<List<AttendeeData>> filteredRosterList() {
    var chunks = [];
    for (var i = 0; i < teacherRosterList.length; i += 10) {
      chunks.add(teacherRosterList.sublist(i, i + 10 > teacherRosterList.length ? teacherRosterList.length : i + 10));
    } //break a list of whatever size into chunks of 10.

    List<Stream<QuerySnapshot>> combineList = [];
    for (var i = 0; i < chunks.length; i++) {
   combineList.add(*[point to your collection]*.where('id', whereIn: chunks[i]).snapshots());
    } //get a list of the streams, which will have 10 each.

    CombineLatestStream<QuerySnapshot, List<QuerySnapshot>> mergedQuerySnapshot = CombineLatestStream.list(combineList);
    //now we combine all the streams....but it'll be a list of QuerySnapshots.

    //and you'll want to look closely at the map, as it iterates, consolidates and returns as a single stream of List<AttendeeData>
    return mergedQuerySnapshot.map(rosterListFromTeacherListDocumentSnapshot);
  }

下面是我如何将其映射以供您参考(为简洁起见,删除了所有字段):

Here's a look at how I mapped it for your reference (took out all the fields for brevity):

  List<AttendeeData> rosterListFromTeacherListDocumentSnapshot(List<QuerySnapshot> snapshot) {
    List<AttendeeData> listToReturn = [];
    snapshot.forEach((element) {
      listToReturn.addAll(element.docs.map((doc) {
        return AttendeeData(
          id: doc.data()['id'] ?? '',
          authorCreatedUID: doc.data()['authorCreatedUID'] ?? '',
        );
      }).toList());
    });
    return listToReturn;
  }

这篇关于颤振Firebase将超过10个Firebase文档放入Stream&amp; lt; List&amp; lt; Map&amp; gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:48
查看更多