Firebase 颤振与振动;Firestore-如何从Firestore的嵌套映射创建类对象
我正在尝试将firestore文档快照映射到我已创建的自定义对象,但执行此操作时出现以下错误: E/flatter(6555):[ERROR:flatter/lib/ui/ui_dart_state.cc(177)]未处理的异常:type'(dynamic)=>TestMap'不是类型的子类型(String,dynamic)=>transform的MapEntry' 据我所知,这与我解析嵌套映射“testMap”的方式有关。是否有任何颤振/火箭弹/飞镖专家能帮助我了解我做错了什么,以及如何解决这个问题 因此,我在Firestore中的数据如下所示:Firebase 颤振与振动;Firestore-如何从Firestore的嵌套映射创建类对象,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在尝试将firestore文档快照映射到我已创建的自定义对象,但执行此操作时出现以下错误: E/flatter(6555):[ERROR:flatter/lib/ui/ui_dart_state.cc(177)]未处理的异常:type'(dynamic)=>TestMap'不是类型的子类型(String,dynamic)=>transform的MapEntry' 据我所知,这与我解析嵌套映射“testMap”的方式有关。是否有任何颤振/火箭弹/飞镖专家能帮助我了解我做错了什么,以及如何解决这
class Categories {
final String category;
final String imageurl;
List testMap = [];
Categories(this.category, this.imageurl);
Map<String, dynamic> toMap() => {
"category": this.category,
"imageurl": this.imageurl,
"testMap": this.testMap
};
Categories.fromMap(Map<String, dynamic> map)
: category = map["category"],
imageurl = map["imageurl"],
testMap = map["testMap"].map((map) { // THE ERROR IS HERE (map)
return TestMap.fromMap(map);
}).toList();
}
class TestMap {
final String v1;
final String v2;
TestMap(this.v1, this.v2);
Map<String, dynamic> toMap() => {
"v1": this.v1,
"v2": this.v2,
};
TestMap.fromMap(Map<dynamic, dynamic> map)
: v1 = map["v1"],
v2 = map["v2"];
}
fromFirestore() {
final FirebaseAuth auth = FirebaseAuth.instance;
Categories categoryObject;
CollectionReference categoriesCollection =
FirebaseFirestore.instance.collection('categories');
categoriesCollection.doc('categories').get()
// ignore: missing_return
.then((docSnapshot) {
for (var i = 1; i <= docSnapshot.data().length; i++) {
print(docSnapshot.data()[i.toString()]['testMap'].toString());
categoryObject = Categories.fromMap(docSnapshot.data()[i.toString()]);
}
});
}
将此数据解析为如下可用对象的两个类对象:
class Categories {
final String category;
final String imageurl;
List testMap = [];
Categories(this.category, this.imageurl);
Map<String, dynamic> toMap() => {
"category": this.category,
"imageurl": this.imageurl,
"testMap": this.testMap
};
Categories.fromMap(Map<String, dynamic> map)
: category = map["category"],
imageurl = map["imageurl"],
testMap = map["testMap"].map((map) { // THE ERROR IS HERE (map)
return TestMap.fromMap(map);
}).toList();
}
class TestMap {
final String v1;
final String v2;
TestMap(this.v1, this.v2);
Map<String, dynamic> toMap() => {
"v1": this.v1,
"v2": this.v2,
};
TestMap.fromMap(Map<dynamic, dynamic> map)
: v1 = map["v1"],
v2 = map["v2"];
}
fromFirestore() {
final FirebaseAuth auth = FirebaseAuth.instance;
Categories categoryObject;
CollectionReference categoriesCollection =
FirebaseFirestore.instance.collection('categories');
categoriesCollection.doc('categories').get()
// ignore: missing_return
.then((docSnapshot) {
for (var i = 1; i <= docSnapshot.data().length; i++) {
print(docSnapshot.data()[i.toString()]['testMap'].toString());
categoryObject = Categories.fromMap(docSnapshot.data()[i.toString()]);
}
});
}
类别{
最终字符串类别;
最终字符串imageurl;
列表testMap=[];
类别(this.category,this.imageurl);
映射toMap()=>{
“类别”:这个类别,
“imageurl”:this.imageurl,
“testMap”:this.testMap
};
类别。fromMap(地图地图)
:category=map[“category”],
imageurl=map[“imageurl”],
testMap=map[“testMap”].map((map){//错误在这里(map)
返回TestMap.fromMap(map);
}).toList();
}
类测试图{
最终字符串v1;
最终字符串v2;
TestMap(this.v1,this.v2);
映射toMap()=>{
“v1”:这是1.v1,
“v2”:这个是.v2,
};
TestMap.fromMap(映射映射)
:v1=映射[“v1”],
v2=地图[“v2”];
}
最后,我从Firestore获得如下数据:
class Categories {
final String category;
final String imageurl;
List testMap = [];
Categories(this.category, this.imageurl);
Map<String, dynamic> toMap() => {
"category": this.category,
"imageurl": this.imageurl,
"testMap": this.testMap
};
Categories.fromMap(Map<String, dynamic> map)
: category = map["category"],
imageurl = map["imageurl"],
testMap = map["testMap"].map((map) { // THE ERROR IS HERE (map)
return TestMap.fromMap(map);
}).toList();
}
class TestMap {
final String v1;
final String v2;
TestMap(this.v1, this.v2);
Map<String, dynamic> toMap() => {
"v1": this.v1,
"v2": this.v2,
};
TestMap.fromMap(Map<dynamic, dynamic> map)
: v1 = map["v1"],
v2 = map["v2"];
}
fromFirestore() {
final FirebaseAuth auth = FirebaseAuth.instance;
Categories categoryObject;
CollectionReference categoriesCollection =
FirebaseFirestore.instance.collection('categories');
categoriesCollection.doc('categories').get()
// ignore: missing_return
.then((docSnapshot) {
for (var i = 1; i <= docSnapshot.data().length; i++) {
print(docSnapshot.data()[i.toString()]['testMap'].toString());
categoryObject = Categories.fromMap(docSnapshot.data()[i.toString()]);
}
});
}
fromFirestore(){
final FirebaseAuth auth=FirebaseAuth.instance;
类别对象;
收集参考分类收集=
FirebaseFirestore.instance.collection('categories');
categoriesCollection.doc('categories').get()
//忽略:缺少返回
.然后((docSnapshot){
对于(var i=1;i您可能需要转换到TestMap
testMap = map["testMap"].map((map) { // THE ERROR IS HERE (map)
return TestMap.fromMap(map);
}).toList().cast<TestMap>;
testMap=map[“testMap”].map((map){//错误在这里(map)
返回TestMap.fromMap(map);
}).toList().cast;