Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase Flatter Firestore我可以使用地图创建收藏吗_Firebase_Flutter_Dart_Google Cloud Firestore - Fatal编程技术网

Firebase Flatter Firestore我可以使用地图创建收藏吗

Firebase Flatter Firestore我可以使用地图创建收藏吗,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我的问题是,我是否可以使用单个文档在多个集合中创建一个文档: firestore .collection('map') .doc('documentvariable') .set(data()); 其中“map”将是一个实际的map,其中包含我想要添加文档的集合的字符串名称列表。 我之所以选择地图,是因为我需要能够使用UI向其添加越来越多的集合。 有可能吗?如果有更好的解决方案吗?这是不可能的,因为集合只接受一个字符串。如注释中提到的其他名称,您可以简单地遍历要添加文档的集合名称

我的问题是,我是否可以使用单个文档在多个集合中创建一个文档:


firestore
  .collection('map')
  .doc('documentvariable')
  .set(data());
其中“map”将是一个实际的map,其中包含我想要添加文档的集合的字符串名称列表。 我之所以选择地图,是因为我需要能够使用UI向其添加越来越多的集合。
有可能吗?如果有更好的解决方案吗?

这是不可能的,因为集合只接受一个字符串。如注释中提到的其他名称,您可以简单地遍历要添加文档的集合名称列表。像这样的

List<String> allCollectionNamesYouWant = ['collection1', 'collection2'];

    for(String collectionName in allCollectionNamesYouWant){
      FirebaseFirestore.instance
          .collection(collectionName)
          .doc('documentvariable')
          .set(data());
      }
List allCollectionNamesYouWant=['collection1','collection2'];
for(所需的AllCollectionNames中的字符串collectionName){
FirebaseFirestore.instance
.集合(集合名称)
.doc('documentvariable')
.set(data());
}

我认为(我的观点)你不能用一个查询在多个集合中创建/编写一个文档。您需要迭代到这些集合名称。但在cloud_firestore中,您可以使用collectionGroup查询直接查找/读取/获取具有相同集合名称的文档。只需循环映射中的所有元素,并将每个循环元素设置为集合(您的_元素)的参数。