My parsing code is like this.
class Model {
List<Info>? info,
idx,
name,
image,
kcal,
effect,
keep,
tip,
youtube01,
youtube02,
youtube03,
season;
Model(
{this.info,
this.idx,
this.name,
this.image,
this.kcal,
this.effect,
this.keep,
this.tip,
this.youtube01,
this.youtube02,
this.youtube03,
this.season});
Model.fromJson(Map<String, dynamic> json)
: idx = json['idx'],
name = json['name'],
image = json['image'],
kcal = json['kcal'],
info = json['info'],
effect = json['effect'],
keep = json['keep'],
tip = json['tip'],
youtube01 = json['youtube01'],
youtube02 = json['youtube02'],
youtube03 = json['youtube03'],
season = json['season'];
Map<String, dynamic> toJson() => {
'idx': idx,
'name': name,
'image': image,
'kcal': kcal,
'info': info,
'effect': effect,
'keep': keep,
'tip': tip,
'youtube01': youtube01,
'youtube02': youtube02,
'youtube03': youtube03,
'season': season,
};
}
And this is the code structure of the body.
void _dataBase() async {
var ref = FirebaseDatabase.instance
.ref("Info/")
.orderByChild("season")
.equalTo("january04");
ref.onValue.listen((DatabaseEvent event) {
dataJson = event.snapshot.value;
//var jsonResult = jsonDecode(dataJson);
var models = Model.fromJson(dataJson);
//print(dataJson);
print(models);
});
}
But I am getting this error in console.
"Unhandled Exception: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>'"
Is the jsonparsing method wrong? No matter how you use jsonDecode or any method, an error appears. please help me how to do it. Thank U.
CodePudding user response:
You can use new Map<String, dynamic>.from(event.snapshot.value);
CodePudding user response:
1st check response is null or not then do this :
Map<dynamic, dynamic> toJson() => { //Change here
'idx': idx,
'name': name,
'image': image,
'kcal': kcal,
'info': info,
'effect': effect,
'keep': keep,
'tip': tip,
'youtube01': youtube01,
'youtube02': youtube02,
'youtube03': youtube03,
'season': season,
};