I am using this logic to convert json data to a custom University model.
import 'dart:convert';
class UniversityWrapper {
UniversityWrapper({
this.isSuccess,
this.message,
this.data,
});
final bool isSuccess;
final dynamic message;
final List<University> data;
factory UniversityWrapper.fromRawJson(String str) => UniversityWrapper.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory UniversityWrapper.fromJson(Map<String, dynamic> json) => UniversityWrapper(
isSuccess: json["is_success"],
message: json["message"],
data: json["data"] == null ? null : List<University>.from(json["data"].map((Map<String, dynamic> x) => University.fromJson(x))),
);
Map<String, dynamic> toJson() => <String, dynamic>{
"is_success": isSuccess,
"message": message,
"data": data == null ? null : List<dynamic>.from(data.map((University x) => x.toJson())),
};
}
class University {
University({
this.id,
this.name,
this.cityId,
});
final int id;
final String name;
final int cityId;
factory University.fromRawJson(String str) => University.fromJson(json.decode(str));
String toRawJson() => json.encode(toJson());
factory University.fromJson(Map<String, dynamic> json) => University(
id: json["id"],
name: json["name"],
cityId: json["city_id"],
);
Map<String, dynamic> toJson() => <String, dynamic>{
"id": id,
"name": name,
"city_id": cityId,
};
}
The problem is in this line:
data: json["data"] == null
? null
: List<University>.from(json["data"].map((Map<String, dynamic> x) => University.fromJson(x))),
If I set x's type as dynamic or nothing, there isn't an error. Like Example 1 and Example 2:
Example 1:
data: json["data"] == null
? null
: List<University>.from(json["data"].map((dynamic x) => University.fromJson(x))),
Example 2:
data: json["data"] == null
? null
: List<University>.from(json["data"].map((x) => University.fromJson(x))),
But the main question is, I don't understand why can't I set Map<String, dynamic> as a type of x. I think that I should be able to set it because University.fromJson method takes Map<String, dynamic>.
The method University.fromJson:
factory University.fromJson(Map<String, dynamic> json) => University(
id: json["id"],
name: json["name"],
cityId: json["city_id"],
);
The error:
E/flutter ( 4780): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type '(Map<String, dynamic>) => University' is not a subtype of type '(dynamic) => dynamic' of 'f'
E/flutter ( 4780): #0 new UniversityWrapper.fromJson (package:hb/models/university.dart:21:76)
E/flutter ( 4780): #1 _ProfilePageState.loadData (package:hb/ui/pages/profile.dart:118:66)
E/flutter ( 4780): <asynchronous suspension>
E/flutter ( 4780):
Edit 1: Example JSON Data:
{
"is_success": true,
"message": null,
"data": [
{
"id": 1,
"name": "Abant İzzet Baysal Üniversitesi",
"city_id": 14
},
{
"id": 118,
"name": "Acıbadem Üniversitesi",
"city_id": 34
},
{
"id": 4,
"name": "Adıyaman Üniversitesi",
"city_id": 2
},
{
"id": 6,
"name": "Afyon Sağlık Bilimleri Üniversitesi",
"city_id": 3
}
]
}