简体   繁体   English

我怎样才能只从 Flutter 中的 Firestore 获取我的消息

[英]How can i get only my messages from firestore in Flutter

I want to create profile page and of course profile page has to be just 1 person posts.我想创建个人资料页面,当然个人资料页面必须只有 1 个人的帖子。 So how we can write the code?那么我们如何编写代码呢?

My main page has all of people posts with this code:我的主页上有所有使用此代码的人的帖子:

@override
  Widget build(BuildContext context) {
     return StreamBuilder(
     // stream: FirebaseFirestore.instance.collectionGroup('Posts').snapshots(),
    
     stream: FirebaseFirestore.instance.collectionGroup('Posts').snapshots(),
      builder: (context, snapshotMsg) {
        if (!snapshotMsg.hasData) {
          return Center(child: CircularProgressIndicator());
        } else {
          return ListView.builder(
            itemCount: snapshotMsg.data.documents.length,
            itemBuilder: (context, index) {
              if (!snapshotMsg.hasData) {
                return Center(child: CircularProgressIndicator());
              } else {
                final dataMsg = snapshotMsg.data.documents[index];
                return FutureBuilder(
                  future: FirebaseFirestore.instance
                      .collection('Users')
                      .doc(dataMsg.reference.parent.parent.id)
                      .get(),
                  builder: (context, snapshotUser) {
                    if (!snapshotMsg.hasData) {
                      return Center(child: CircularProgressIndicator());
                    } else {
                      final dataUser = snapshotUser.data;
                      if (dataUser == null) {
                        return Center(child: CircularProgressIndicator());
                      } else {
                        return homePageCard(context,dataMsg: dataMsg, dataUser: dataUser,);
                      }
                    }
                  },
                );
              }
            },
          );
        }
      });
  }

But as a said, this code has all of users posts.但正如所说,这段代码包含所有用户的帖子。 What should i change in here for seeing just my posts?我应该在这里改变什么才能看到我的帖子?

( I tried to stream:FirebaseFirestore.instance.collection('Users').doc().collection('Posts').snapshots() this code but it gives error... ) (我试图stream:FirebaseFirestore.instance.collection('Users').doc().collection('Posts').snapshots()这个代码,但它给出了错误......)

my firebase screenshots:我的 firebase 截图: 我的火力基地截图:

在此处输入图像描述

If you want to get only the posts of the current connected user you should use Firebase Auth :如果您只想获取当前连接用户的帖子,您应该使用Firebase Auth

final FirebaseAuth auth = FirebaseAuth.instance;

// Previously get the current connected user uid
    final User user = auth.currentUser;
    final uid = user.uid;

// ...

// Get the post of the current connected user
FirebaseFirestore.instance.collection('Users').doc(uid).collection('Posts').snapshots()

If you want to get the posts of a specific user (not the one connected), just replace the uid of the current user.如果您想获取特定用户(不是连接的用户)的帖子,只需替换当前用户的 uid。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM