如何从 Firebase 集合中的多个文档中获取数据

时间:2021-07-06 10:15:59

标签: firebase flutter dart

我正在获取用户的位置,然后使用该位置获取包含该位置的文档,然后在页面上的文档中显示数据。 我的 Firebase Database 的结构是 /posts/{userId}/userPosts/{postId}。我希望它搜索所有帖子(意思是搜索所有不同的 postId)。

Future<void> initState() {
    super.initState();
    getListings();
  }

  getListings() async {
    placemark = await getUserLocation();
    getLocationListings(placemark);
  }

  getLocationListings(placemark) async {
    setState(() {
      isLoading = true;
    });
    QuerySnapshot snapshot = await FirebaseFirestore.instance.collection('posts')
        // .doc(userId)
        // .collection('userPosts')
        .where('location', isGreaterThanOrEqualTo: placemark.locality)
        .get();
    setState(() {
      posts = snapshot.docs.map((doc) => Post.fromDocument(doc)).toList();
      isLoading = false;
    });
    // print(placemark.locality);
  }

当我按原样运行代码时(.doc(userId).collection('userPosts') 已注释掉),列表 List<Post> posts = []; 保持为空。但是,当我取消对它们的注释并提供 userId 时,我会得到所有包含 placemark.locality 的文档。我怎么能绕过这个? enter image description here

0 个答案:

没有答案