Flutter仅在Firestore中创建documentID与数组中的String匹配的项目 [英] Flutter Only create item which documentID matches String in array in Firestore

查看:80
本文介绍了Flutter仅在Firestore中创建documentID与数组中的String匹配的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是Flutter和Firestore的新手,并且遇到一个问题,我基本上只想在列表中[c1>也(且仅当)documentID也出现时,才用集合"brites"中的项目填充列表. >在另一个 Firestore集合中的数组中,该集合称为"users"> "bookmarks"> array(包含带有文档ID:s的Strings).

I'm still new to Flutter and Firestore, and have a problem where I basically want to only populate a list with items in the collection "brites" if (and only if) the documentID also appear as a String in an array in another Firestore collection called "users" > "bookmarks" > array (containing Strings with documentID:s).

我很难知道从哪里开始,我认为这主要是由于我目前对Firestore和异步流和查询的理解还很模糊.也许where()map()是解决方案,但是具体问题目前在我头上.

I have a hard time knowing where to start, I believe mainly due to my currently vague understanding of Firestore and asynchronous streams and queries. Maybe where() or map() is the solution, but what that is concrete is over my head currently.

List<Widget> populateBriteList(AsyncSnapshot snapshot, int x, bool isBookmarksSection) {
  List<Widget> list = new List<Widget>();

    BriteShowItem _briteContent(j) {
      return BriteShowItem(
        briteID: snapshot.data.documents[j].documentID,
        imagePath: snapshot.data.documents[j]["imagePath"],
        title: snapshot.data.documents[j]["title"],
        author: snapshot.data.documents[j]["author"],
        published: snapshot.data.documents[j]["published"],
        duration: snapshot.data.documents[j]["duration"],
        isBookmarked: snapshot.data.documents[j]["isBookmarked"],
      );
    }

    if (isBookmarksSection) {
      for (int i=0; i<x; i++) {
        //Here only list.add briteContent(i) if the "documentID" in passed in 
        //Snapshot ("brites" collection)
        //is equal to a string in "bookmarks" array in "users" collection
        list.add(
          _briteContent(i)
        );
      }
    } else {
      for (int i=0; i<x; i++) {
        list.add(
          _briteContent(i)
        );
      }
    }
  return list;
}

推荐答案

所以一件事是,当您实际构建依赖于Firestore中某些数据的小部件时,最终您已经需要该数据.这并不意味着您在等待Future解析时无法返回临时值.但是,在这种情况下,看起来您一旦拥有数据就好像在调用此方法.因此,也许也可以直接传入书签数组(也就是说,像这样的带有大量参数的方法可以很好地表明事情已经失控,需要进行更多的结构更改,例如,通过拆分方法取决于条件,并根据需要调用不同的方法):

So one thing is that when you are actually building the widgets that depend on some data in firestore, you ultimately already need to have that data. That doesn't mean you can't return a temporary value while you are waiting for a Future to resolve. But, in this case, it looks like you are calling this method once you already have the data. So maybe just pass the array of bookmarks in, also (that said, a method like this with a bunch of arguments is a good indicator that things are getting out of hand and some more structural changes are needed - for example, by splitting up the method depending on the conditions and calling different methods as needed):

List<Widget> populateBriteList(AsyncSnapshot snapshot, int x, bool isBookmarksSection, AsyncSnapshot bookmarkSnapshot) {

...

if (isBookmarksSection) {
  for (int i=0; i<x; i++) {
    if(bookmarkSnapshot.documents.where((document) => condition(document.data)).length > 0){
      list.add(
        _briteContent(i)
      );
    }
  }

在这种情况下,在可迭代列表(文档)上调用"where(someTest)",每个元素都传递到someTest方法中,如果结果为true,则将该元素传递到列表中.因此,如果列表大于0,则至少一个元素满足该条件.

In this case, 'where(someTest)' is called on an iterable list, (documents), each element is passed into the someTest method, and if the result is true, that element is passed into the list. So if the list is greater than 0, at least one element satisfied that condition.

这篇关于Flutter仅在Firestore中创建documentID与数组中的String匹配的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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