获取用户文档 ID,在Flutter中向Firebase Firestore添加记录。

huangapple go评论47阅读模式
英文:

how to get user document id while adding record to firebasefirestore in flutter

问题

I am created a basic app for adding post where i need to store post id

我创建了一个基本的应用程序用于添加帖子,我需要存储帖子的ID

I dont want to generate any id my own way, to know if there is a another way

我不想以我自己的方式生成任何ID,想知道是否有其他方法

here is my coding for adding post

这是我用于添加帖子的代码

IconButton(onPressed: () async {
                    await FirebaseFirestore.instance.collection("posts").doc().set(
                        {
                          'message': txtpost.text,
                          'postby': FirebaseAuth.instance.currentUser!.email,
                          'posttime': Timestamp.now(),
                          'likes':[],
                          'postid':?????// here how to get current document id
                        });
                    print('Post Added Successfully...');
                    txtpost.clear();

                  }, icon: Icon(Icons.send))
IconButton(onPressed: () async {
                    await FirebaseFirestore.instance.collection("posts").doc().set(
                        {
                          'message': txtpost.text,
                          'postby': FirebaseAuth.instance.currentUser!.email,
                          'posttime': Timestamp.now(),
                          'likes':[],
                          'postid':?????// 这里如何获取当前文档的ID
                        });
                    print('Post Added Successfully...');
                    txtpost.clear();

                  }, icon: Icon(Icons.send))

In your code, you're trying to add a post to Firestore and want to know how to get the current document ID. The ID will be automatically generated when you use .doc() without specifying an ID. You can access it like this:

在您的代码中,您正在尝试将帖子添加到Firestore,并想知道如何获取当前文档的ID。如果您在使用.doc()时没有指定ID,ID将自动生成。您可以像这样访问它:

IconButton(onPressed: () async {
  final DocumentReference docRef = await FirebaseFirestore.instance.collection("posts").add({
    'message': txtpost.text,
    'postby': FirebaseAuth.instance.currentUser!.email,
    'posttime': Timestamp.now(),
    'likes': [],
  });
  print('Post Added Successfully. Document ID: ${docRef.id}');
  txtpost.clear();
}, icon: Icon(Icons.send))

This code uses .add() to add a document to the "posts" collection, and it will automatically generate a unique ID for the document.

英文:

I am created a basic app for adding post where i need to store post id

I dont want to generate any id my own way, to know if there is a another way

here is my coding for adding post

IconButton(onPressed: () async {
                    await FirebaseFirestore.instance.collection("posts").doc().set(
                        {
                          'message':txtpost.text,
                          'postby':FirebaseAuth.instance.currentUser!.email,
                          'posttime':Timestamp.now(),
                          'likes':[],
                          'postid':?????// here how to get current document id
                        });
                    print('Post Added Successfully...');
                    txtpost.clear();

                  }, icon: Icon(Icons.send))

答案1

得分: 1

以下是翻译好的内容:

您可以在没有提供路径时调用函数 doc 时自动生成的 ID。

因此,您可以首先获取 doc 的引用,然后存储帖子。

以下是代码的示例:

IconButton(
  onPressed: () async {
    final docRef =
        FirebaseFirestore.instance.collection('posts').doc();
    final docId = docRef.id;
    docRef.set(
      {
        'message': txtpost.text,
        'postby': FirebaseAuth.instance.currentUser!.email,
        'posttime': Timestamp.now(),
        'likes': [],
        'postid': docId
      },
    );
    print('帖子已成功添加...');
    txtpost.clear();
  },
  icon: Icon(Icons.send),
),
英文:

You could use the ID that is automatically generated when calling the function doc when no path is provided.

Therefore you could first get the reference of the doc and then store the post.

Here is how the code could look like:

    IconButton(
            onPressed: () async {
              final docRef =
                  FirebaseFirestore.instance.collection('posts').doc();
              final docId = docRef.id;
              docRef.set(
                {
                  'message': txtpost.text,
                  'postby': FirebaseAuth.instance.currentUser!.email,
                  'posttime': Timestamp.now(),
                  'likes': [],
                  'postid': docId
                },
              );
              print('Post Added Successfully...');
              txtpost.clear();
            },
            icon: Icon(Icons.send),
          ),

答案2

得分: 1

IconButton(
onPressed: () async {
final documentRef = FirebaseFirestore.instance.collection('posts').doc();
final documentId = documentRef.id;
await documentRef.set(
{
'message': txtpost.text,
'postby': FirebaseAuth.instance.currentUser!.email,
'posttime': Timestamp.now(),
'likes': [],
'postid': documentId
}
);
print('Post Added Successfully...');
txtpost.clear();
},
icon: Icon(Icons.send),
)

英文:

The code below should do it:

IconButton(
        onPressed: () async {
          final documentRef = FirebaseFirestore.instance.collection('posts').doc();
          final documentId = documentRef.id;
              await documentRef.set(
                  {
                    'message':txtpost.text,
                    'postby':FirebaseAuth.instance.currentUser!.email,
                    'posttime':Timestamp.now(),
                    'likes':[],
                    'postid': documentId
                  });
              print('Post Added Successfully...');
              txtpost.clear();
        },
        icon: Icon(Icons.send),
      ),

Every document reference comes with an id. You can get this id and set it as the post id.

huangapple
  • 本文由 发表于 2023年6月18日 18:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定