簡體   English   中英

flutter 從 firestore 中引入嵌套集合

[英]flutter Bring nested collection from firestore

我正在開發 flutter 應用程序,我創建了名為“publicPost”的集合,其中包含文檔,然后我有另一個嵌套集合及其文檔,如圖所示。

問題是當我試圖從“publicPost”中檢索並顯示所有數據時,我做不到。 我需要指定一個文檔,它只會帶來一個文檔的數據。 我怎樣才能從主要收藏中取出所有文件。

這是我保存數據的代碼

 sendData() async {
    final _documentReference = FirebaseFirestore.instance
        .collection("publicPosts")
        .doc(FirebaseAuth.instance.currentUser!.uid);
    final _collectionReference = _documentReference.collection("postlist");
    Map<String, dynamic> userInfoMap = {
      "address": _addressController.text,
      "bloodType": neVal,
      "place": _placeController.text,
      "post": _postController.text,
      "date": FieldValue.serverTimestamp(),
      "username": FirebaseAuth.instance.currentUser!.email,
      "name": '$firstNmae $lastName',
    };
    await _collectionReference.add(userInfoMap);
    if (mounted) {
      Navigator.pop(context);
    }
  }

這就是我如何嘗試使用此代碼進行檢索但我沒有工作

final Stream<QuerySnapshot> _usersStream = FirebaseFirestore.instance
      .collection('publicPosts')
      .doc()
      .collection('postlist')
      .snapshots(); 

誰能幫忙解決這個問題?

在此處輸入圖像描述

要從所有postlist collections 加載文檔,您可以使用集合組查詢

FirebaseFirestore.instance
      .collectionGroup('postlist')
      .snapshots(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM