简体   繁体   English

没有为类型“QuerySnapshot”定义吸气剂“文档” <map<string, dynamic> >' </map<string,>

[英]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.

相关问题 没有为类型“QuerySnapshot”定义吸气剂“文档”<object?> '</object?> - The getter 'documents' isn't defined for the type 'QuerySnapshot<Object?>' 'Future 类型的值<querysnapshot<map<string, dynamic> &gt;&gt;' 不能分配给“QuerySnapshot”类型的变量<object?> ' </object?></querysnapshot<map<string,> - A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>' 没有为“对象”类型定义 getter“文档”- Flutter 错误 - The getter 'documents' isn't defined for the type 'Object' - Flutter Error getter 'bodyBytes' 没有为 'Future' 类型定义<response>函数(URI,{地图<string, string> ? 标题})'</string,></response> - The getter 'bodyBytes' isn't defined for the type 'Future<Response> Function(Uri, {Map<String, String>? headers})' 应为“QuerySnapshot”类型的值<object?> ',但得到的是 '_MapStream 类型之一<querysnapshotplatform, querysnapshot<map<string, dynamic> >>'</querysnapshotplatform,></object?> - Expected a value of type 'QuerySnapshot<Object?>', but got one of type '_MapStream<QuerySnapshotPlatform, QuerySnapshot<Map<String, dynamic>>>' 没有为类型“FirebaseAuth”定义吸气剂“AuthStateChanges” - The getter 'AuthStateChanges' isn't defined for the type 'FirebaseAuth' 错误:没有为 class 'Object' 定义 getter 'documents' - Error: The getter 'documents' isn't defined for the class 'Object' 未为类型“QuerySnapshot”定义运算符“[]”<object?> ' 尝试定义运算符 '[]' - Flutter</object?> - The operator '[]' isn't defined for the type 'QuerySnapshot<Object?>' Try defining the operator '[]' - Flutter Flutter Fire Type cast AsyncSnapshot <querysnapshot<map<string, dynamic> >> 列表<dynamic> ? </dynamic></querysnapshot<map<string,> - Flutter Fire Type cast AsyncSnapshot<QuerySnapShot<Map<String, dynamic>>> to List<dynamic>? firestore getter 'length' 不是为类型 'DocumentSnapshot' 定义的<object?> '</object?> - firestore the getter 'length' isn't defined for the type 'DocumentSnapshot<Object?>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM