【发布时间】:2021-06-08 15:53:26
【问题描述】:
我一直在尝试将多个 Firestore 文档从一个集合添加到我的颤振应用程序中的另一个集合中,到目前为止,这是我最接近的一次。
我尝试手动创建从 Firestore 数据读取的地图,然后使用 forEach() 将其存储回另一个集合中的 Firestore,但失败了。
我在下面使用了相同的函数,forEach() 一个带有硬编码数组的函数:
var myArray69 = [
{
'name': 'a',
'brand': 'bruh'
},
{
'name': 'b'
}
];
myArray69.forEach((doc) => {
FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});
并且它将每个数组作为文档添加到 Firestore 中就好了,所以我一直在尝试使用来自另一个集合的数据来复制它,但没有运气。
这是我的代码:
Widget _foodList(){
return Expanded(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('foodShelf')
.snapshots(),
builder: buildFoodList,
),
);
}
Widget buildFoodList(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.docs.length,
itemBuilder: (BuildContext context, int index) {
DocumentSnapshot food = snapshot.data.docs[index];
var foodName = food.get('foodName');
var someMap = {
"a": foodName,
"b": '2',
};
return GestureDetector(
key: Key(food.id),
onTap: () {
someMap.forEach((doc) => { // <----- First error is here
FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});
print(someMap);
},
child: ListTile(
title: Text(foodName),
),
);
}
);
}
如果我注释掉这一部分:
someMap.forEach((doc) => {
FirebaseFirestore.instance.collection('myOtherCollection69').add(doc)});
只留下print(someMap);,然后我点击屏幕上的一个结果,我在控制台中得到了地图:
I/flutter ( 6922): {a: Pure 100% florida orange juice, b: 2}
但我无法使用此代码将其添加到 Firestore,我收到以下错误:
- 第一个错误:
The argument type 'Set<Future<DocumentReference<Map<String, dynamic>>>> Function(String)' can't be assigned to the parameter type 'void Function(String, dynamic)'.dart(argument_type_not_assignable)
- 第二个错误:
The argument type 'String' can't be assigned to the parameter type 'Map<String, dynamic>'.dart(argument_type_not_assignable)
任何帮助将不胜感激!
【问题讨论】:
标签: firebase flutter dart google-cloud-firestore