0

我正在尝试从左侧获取当前登录用户的所有 postID,并且我想使用此信息来获取包含相同 postID 的所有帖子。我怎样才能做到这一点?

到目前为止,我创建了一个函数,可以提取当前用户的所有 postID 并将其放入列表中,但我认为我无法使用它。 https://i.stack.imgur.com/ThfSm.png

  List<String> myFavoritePosts = [];
  Future<List<String>> getFavorites() async {
    final FirebaseAuth auth = FirebaseAuth.instance;
    final User? user = auth.currentUser;
    final userID = user!.uid;
    var result = await FirebaseFirestore.instance
        .collection('favorites')
        .where("userID", whereIn: [userID]).get();
    for (var res in result.docs) {
      myFavoritePosts.add((res.data()['postID'] ?? ''));
    }

    return myFavoritePosts;
  }

但我无法将它实现到我的 StreamBuilder 结构中。

  Widget build(BuildContext context) {
    final Stream<QuerySnapshot> _postStream = FirebaseFirestore.instance
        .collection('Posts') 
        .where('postID',
            isEqualTo:
                getFavorites())
        .snapshots();

    return StreamBuilder<QuerySnapshot>(
        stream: _postStream,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasError) {
            return Text('Please try again');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Text("Loading");
          }

          return ListView(
        children: snapshot.data!.docs.map((DocumentSnapshot document) {
          Map<String, dynamic> data =
              document.data()! as Map<String, dynamic>;
          return ListTile(
            title: Text(data['title']),
            subtitle: Column(
              children: <Widget>[
                Text(data['details']),
              ],
            ),
          );
        }).toList(),
      );

如何连接这两个数据库,以便显示具有相同 ID 的帖子?

4

1 回答 1

0

用这个:-

`

List<String> myFavoritePosts = [];
  Future<List<String>> getFavorites() async {
    var favourite;
    final FirebaseAuth auth = FirebaseAuth.instance;
    final User? user = auth.currentUser;
    final userID = user!.uid;
    var result = await FirebaseFirestore.instance
        .collection('favorites')
        .where("userID", whereIn: [userID]).get();
    for (var res in result.docs) {
      myFavoritePosts.add((res.data()['postID'] ?? ''));
    }
    myFavoritePosts.forEach((element){
         favourite = element;
 
});
return favourite;
}`

接着:-

'Widget build(BuildContext context) {
final Stream<QuerySnapshot> _postStream = FirebaseFirestore.instance
    .collection('Posts') 
    .where('postID',
        isEqualTo:
            getFavorites())
    .snapshots();

return StreamBuilder<QuerySnapshot>(
    stream: _postStream,
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.hasError) {
        return Text('Please try again');
      }

      if (snapshot.connectionState == ConnectionState.waiting) {
        return Text("Loading");
      }

      return ListView(
    children: snapshot.data!.docs.map((DocumentSnapshot document) {
      Map<String, dynamic> data =
          document.data()! as Map<String, dynamic>;
      return ListTile(
        title: Text(data['title']),
        subtitle: Column(
          children: <Widget>[
            Text(data['details']),
          ],
        ),
      );
    }).toList(),
  );'
于 2022-01-01T19:36:30.787 回答