dart - Flutter Firestore 添加具有自定义 ID 的新文档
问题描述
如何使用 Dart 和 Flutter 添加具有自定义 id 的新文档?
PS:我可以使用此代码将新文档添加到集合中,但其 id 随机设置
postRef.add(data);
这
postRef
是CollectionReference
和data
是Map<String, dynamic>
解决方案
您可以使用set()
function 而不是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.doc(postID).set(postData);
我希望对任何人都有帮助。
推荐阅读
- octoprint - 如何验证 Octoprint 不能/不会将我的 RaspberryPi 变成恶意软件?
- asp.net - 在 Asp .Net 和 Asp .Net Core 之间共享身份验证
- angular - Internet Explorer 中的 arcgis js API 4.7 错误-“未检测到本机 wasm 支持”
- kubernetes - kubernetes go-client PersistentVolumeClaim 未按请求配置,卡在 Pending 状态
- powershell - 比较两个不同 Excel 工作表中的多列
- c - 在条件语句中使用二进制文件中的数据
- reactjs - 反应状态是按钮点击后的一种状态
- php - 具有挑战性的 SQL 查询
- sql - 在 Apache Calcite 中将带有聚合函数的 SQL 查询转换为关系代数表达式 - 找不到函数签名的匹配项
- tfs - 项目中所有黄瓜测试场景的测试用例标题在 TFS 中显示为“特征”