[英]The getter 'documents' isn't defined for the type 'QuerySnapshot<Map<String, dynamic>>'
The type 'List<QueryDocumentSnapshot<Map<String, dynamic>>>' used in the 'for' loop must implement 'Iterable' with a type argument that can be assigned to 'Map<dynamic, dynamic>'.dart(for_in_of_invalid_element_type) Changed from this code: “for”循环中使用的类型“List<QueryDocumentSnapshot<Map<String, dynamic>>>”必须使用可分配给“Map<dynamic, dynamic>”的类型参数实现“Iterable”。dart(for_in_of_invalid_element_type)从此代码更改:
class CategoryServices {
String collection = "categories";
Firestore _firestore = Firestore.instance;
Future<List<CategoryModel>> getCategories() async =>
_firestore.collection(collection).getDocuments().then((result) {
List<CategoryModel> categories = [];
for (DocumentSnapshot category in result.documents) {
categories.add(CategoryModel.fromSnapshot(category));
}
return categories;
});
}
To this, but getting an error.对此,却报错。 Where did I go wrong?
我go哪里错了?
class CategoryModel {
String? image;
String? name;
CategoryModel({this.image, this.name});
// receiving data from server
factory CategoryModel.fromMap(map) {
return CategoryModel(image: map['image'], name: map['name']);
}
// sending data to our server
Map<String, dynamic> toMap() {
return {
'image': image,
'name': name,
};
}
}
class CategoryServices {
String collection = "category";
FirebaseFirestore _firestore = FirebaseFirestore.instance;
Future<List<CategoryModel>> getCategories() async =>
_firestore.collection(collection).get().then((result) {
List<CategoryModel> categories = [];
for (Map category in result.docs) { //error at this line
categories.add(CategoryModel.fromMap(category));
}
return categories;
});
}
The elements of result.docs
are QueryDocumentSnapshot
, and not a Map
. QueryDocumentSnapshot
result.docs
而不是Map
。
I think you're looking for:我想你正在寻找:
for (QueryDocumentSnapshot category in result.docs) {
categories.add(CategoryModel.fromMap(category.data()));
}
return categories;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.