【发布时间】:2021-11-19 13:55:55
【问题描述】:
“for”循环中使用的类型“List
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;
});
}
对此,但出现错误。我哪里做错了?
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;
});
}
【问题讨论】:
标签: android firebase flutter dart mobile-application