首页 > 解决方案 > Flutter Firebase 如何创建新集合?

问题描述

如何在我的 Firebase Cloud Firestore 中创建一个新集合,并在我的服务文件中使用 dart 和 flutter 函数创建一个新集合?我搜索了它,但我只能找到如何将数据添加到 firebase 中的现有集合。有人能帮我吗?请:)

标签: flutterdartgoogle-cloud-firestore

解决方案


来自 Cloud Firestore 文档(此处):

Cloud Firestore 将数据存储在 Documents 中,这些 Documents 存储在 Collections 中。Cloud Firestore 会在您首次向文档添加数据时隐式创建集合和文档。您不需要显式创建集合或文档。

因此,您需要做的是添加到以下集合中,您的集合将被创建:

Firestore.instance.collection("you_Collection_Path").add({
  "key":value //your data which will be added to the collection and collection will be created after this
}).then((_){
  print("collection created");
}).catchError((_){
  print("an error occured");
});

推荐阅读