简体   繁体   English

Flutter 如何以另一种方法从 Firestore 中传入documents.length?

[英]Flutter How to pass in documents.length from Firestore in another method?

I have a method which takes an int value, which is supposed to be the value of the length of an array field in a document in a collection in Cloud Firestore .我有一个采用int值的方法,该值应该是Cloud Firestore集合文档array字段长度的值。

I'm very new to both Flutter and especially Firestore, and I figure it has something to do with how futures, queries and streams works, but I can't seem to understand it correctly.我对 Flutter 尤其是 Firestore 都很陌生,我认为这与期货、查询和流的工作方式有关,但我似乎无法正确理解它。

This is what I'm trying to achieve:这就是我想要实现的目标:

class UserBookmarksSection extends StatefulWidget {
  @override
  _UserBookmarksSectionState createState() => _UserBookmarksSectionState();
}

class _UserBookmarksSectionState extends State<UserBookmarksSection> {  
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,      
      children: <Widget>[
        HeadlineItem(title: "Your bookmarks"), 
        StreamBuilder(
          stream: Firestore.instance.collection("brites").snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text("Loading...");
            return Column(
              children: populateBriteList(snapshot, x, true), //Here I want to pass in the length of the array in firestore into x
            );
          }
        ),
      ],
    );
  }
}

Basically I've tried to create a method which returns a Future<int> and tried to pass that, but that didn't work because a Future<int> isn't the same type as int .基本上,我尝试创建一个返回Future<int>的方法并尝试传递它,但这不起作用,因为Future<int>int类型不同。 That I understand, but the solution is not clear to me.我明白,但解决方案对我来说并不清楚。

I'm not sure the relationship between brites and bookmarks- that is, which you expect to change more often.我不确定 brites 和书签之间的关系——也就是说,你希望更频繁地改变它。 I'm going to assume that the bookmarks isn't changing once the user opens the bookmarks section.我将假设一旦用户打开书签部分,书签就不会改变。

If that is the case you can retrieve the bookmarks length value in initState.如果是这种情况,您可以在 initState 中检索书签长度值。 You will override the state class's initState() method, and then call an async method which retrieves the value from the database.您将覆盖 state 类的 initState() 方法,然后调用从数据库中检索值的异步方法。 (init state can't be async itself). (init state 本身不能是异步的)。 Once the value is retrieved, you can then call setState() to update the widget with the value of bookmarks set as an int.检索到值后,您可以调用 setState() 以使用设置为 int 的书签值更新小部件。

Might look something like this:可能看起来像这样:

class UserBookmarksSection extends StatefulWidget {
  @override
  _UserBookmarksSectionState createState() => _UserBookmarksSectionState();
}

class _UserBookmarksSectionState extends State<UserBookmarksSection> {  
  int bookmarksLength;

  @override
  void initState(){
    super.initState();
    getBookmarksLength();
  }

  Future<void> getBookmarksLength() async {
    bookmarksLength = await getArrayLength(id);
    setState((){});
  }

  @override
  Widget build(BuildContext context) {
    if(bookmarksLength == null) return CircularProgressIndicator();
    else return Column(
      crossAxisAlignment: CrossAxisAlignment.start,      
      children: <Widget>[
        HeadlineItem(title: "Your bookmarks"), 
        StreamBuilder(
          stream: Firestore.instance.collection("brites").snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text("Loading...");
            return Column(
              children: populateBriteList(snapshot, bookmarksLength, true), 
            );
          }
        ),
      ],
    );
  }
}


Firestore firestore = Firestore.instance;

Future<int> getArrayLength(String documentId) async {
  DocumentSnapshot snapshot = await firestore.collection('collection').document(documentId).get();
  return snapshot.data['array'].length;
}

A more robust solution might be to have a separate class that handles both listening to the stream and retrieving the length of the array, and then combining all of the information you're going to display into some abstract data type which you put into a separate stream you specify that is specifically for the UI to listen to.一个更强大的解决方案可能是拥有一个单独的 class 来处理监听 stream 和检索数组的长度,然后将您要显示的所有信息组合成一些抽象数据类型,您将其放入单独的您指定的 stream 是专门供 UI 监听的。

暂无
暂无

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

相关问题 无法使用documents.length 检索我的firebase 中的文档 - cant retreive the documents in my firebase using documents.length 如何从保存在另一个集合中的 id 作为 flutter firestore 中的文档检索存储在一个集合中的文档 - How to retrieve documents stored in one collection from the ids saved in another collection as documents in flutter firestore 如何列出 Flutter Firestore 中文档的值? - How to list values ​from documents in Flutter Firestore? Flutter 和 Firestore:如何从集合组更新多个特定文档 - Flutter and Firestore: How to update multiple specific documents from collection group 如何将数据从 flutter 中的 Firestore 传递到 Typeahead - How to pass data to the Typeahead from firestore in flutter 如何从 Firestore 中 flutter 中的方法访问详细信息 - How to access details from a method in flutter in firestore 我如何以列表形式从云 Firestore 显示数据并传递到 flutter 中的另一个文件 - How i can display data in form of list from cloud firestore and pass to another file in flutter 如何在 Firestore -Flutter 中将所有文档从一个集合复制到另一个集合? - How to copy all documents from one collection to other in Firestore -Flutter? 无法从 flutter 上的 Firestore 获取文档 - Can't get documents from firestore on flutter 如何创建一个 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 监听从 Flutter 中的文档引用列表创建的多个 Firestore 文档 - How to create one stream listening to multiple Firestore documents created from list of documents references in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM