Flutter Firestore使用自定义ID添加新文档 [英] Flutter Firestore add new document with Custom ID

查看:75
本文介绍了Flutter Firestore使用自定义ID添加新文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Dart和Flutter添加具有自定义ID的新文档?

How to add new document with custom id using Dart and Flutter?


PS:我可以使用以下命令将新文档随机添加到集合中,但其ID集是随机的这段代码

PS: I can add new document to collection but its id sets randomly, using this code

postRef.add(data);

其中 postRef CollectionReference ,而 data Map< String,动态>


推荐答案

您可以使用 set 函数,而不是 add

You can use set function instead of add.

这里是完整代码:

final CollectionReference postsRef = Firestore.instance.collection('/posts');

var postID = 1;

Post post = new Post(postID, "title", "content");
Map<String, dynamic> postData = post.toJson();
await postsRef.document(postID).setData(postData);

我希望能帮助任何人。

这篇关于Flutter Firestore使用自定义ID添加新文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆