I am new in Flutter. I have tried to developed Model class in dart. but always show the error message
Exception:type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String', tt:#0 new User.fromJson
I don't understand, where my code will error. I want to solutions.
I want to convert model class in dart.
[
{
"route": {
"routeID": "aaaaaaaa",
"routeTitle": "bbbbbbbb"
},
"distributor": {
"distributorID": "cccccccccccc",
"distributorName": "ddddddddddd"
},
"visitDate": null
}
]
I have been tried to my source code. like below code
import 'dart:convert';
List<User> userFromJson(String str) =>
List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
String userToJson(List<User> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class User {
User({
this.visitDate,
this.route,
this.distributor,
});
String visitDate;
String route;
String distributor;
factory User.fromJson(Map<String, dynamic> json) => User(
visitDate: json["visitDate"],
route: json["route"],
distributor: json["distributor"],
);
Map<String, dynamic> toJson() => {
"visitDate": visitDate,
"route": route,
"distributor": distributor,
};
}
Route RouteFromJson(String str) => Route.fromJson(json.decode(str));
String RouteToJson(Route data) => json.encode(data.toJson());
class Route {
Route({
this.routeID,
this.routeTitle,
});
String routeID;
String routeTitle;
factory Route.fromJson(Map<String, dynamic> json) => Route(
routeID: json["routeID"],
routeTitle: json["routeTitle"],
);
Map<String, dynamic> toJson() => {
"routeID": routeID,
"routeTitle": routeTitle,
};
}
Distributor DistributorFromJson(String str) => Distributor.fromJson(json.decode(str));
String DistributorToJson(Distributor data) => json.encode(data.toJson());
class Distributor {
Distributor({
this.distributorID,
this.distributorName,
});
String distributorID;
String distributorName;
factory Distributor.fromJson(Map<String, dynamic> json) => Distributor(
distributorID: json["distributorID"],
distributorName: json["distributorName"],
);
Map<String, dynamic> toJson() => {
"distributorID": distributorID,
"distributorName": distributorName,
};
}
how to correct my model class. please help me. thanks
CodePudding user response:
Change your User class like this :
class User {
User({
this.visitDate,
this.route,
this.distributor,
});
String visitDate;
Route route;
Distributor distributor;
factory User.fromJson(Map<String, dynamic> json) => User(
visitDate: json["visitDate"],
route = json['route'] != null ? Route.fromJson(json['route']) : null;
distributor = json['distributor'] != null
? Distributor.fromJson(json['distributor'])
: null;
);
Map<String, dynamic> toJson() => {
"visitDate": visitDate,
if (route != null) {
data['route'] = this.route.toJson();
}
if (distributor != null) {
data['distributor'] = this.distributor.toJson();
}
};
}
CodePudding user response:
You can also check out the json_serializable
package for that.
This way, you don't have to write this code yourself.
https://pub.dev/packages/json_serializable
CodePudding user response:
This is how you can rewrite the model:
import 'dart:convert';
List<User> usersFromJson(String str) =>
List<User>.from(json.decode(str).map((x) => User.fromJson(x)));
String usersToJson(List<User> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class User {
User({
required this.visitDate,
required this.route,
required this.distributor,
});
String? visitDate;
Route route;
Distributor distributor;
User.fromJson(Map<String, dynamic> json)
: visitDate = json["visitDate"],
route = Route.fromJson(json["route"]),
distributor = Distributor.fromJson(json["distributor"]);
Map<String, dynamic> toJson() => {
"visitDate": visitDate,
"route": route.toJson(),
"distributor": distributor.toJson(),
};
}
class Route {
Route({
required this.routeID,
required this.routeTitle,
});
String routeID;
String routeTitle;
Route.fromJson(Map<String, dynamic> json)
: routeID = json["routeID"],
routeTitle = json["routeTitle"];
Map<String, dynamic> toJson() => {
"routeID": routeID,
"routeTitle": routeTitle,
};
}
class Distributor {
Distributor({
required this.distributorID,
required this.distributorName,
});
String distributorID;
String distributorName;
Distributor.fromJson(Map<String, dynamic> json)
: distributorID = json["distributorID"],
distributorName = json["distributorName"];
Map<String, dynamic> toJson() => {
"distributorID": distributorID,
"distributorName": distributorName,
};
}
void main() {
const data = '''[
{
"route": {
"routeID": "aaaaaaaa",
"routeTitle": "bbbbbbbb"
},
"distributor": {
"distributorID": "cccccccccccc",
"distributorName": "ddddddddddd"
},
"visitDate": null
}
]''';
final decoded = usersFromJson(data);
final encoded = usersToJson(decoded);
print(decoded);
print(encoded);
}
Some things to note.
You appear to be using an outdated version of dart. I would suggest updating the version of dart in your project to 2.14.0
which is currently the latest major version. You might have to run flutter upgrade
to do so. You can change the version of dart in your pubspec.yaml
file. The setting for your dart version will look something like this:
environment:
sdk: ">=2.14.0 <3.0.0"
The main problem you have in your code is that within your User
class you have defined route
and distributor
to be of type String
when really, you want them to be of type Route
and Distributor
. Within the constructor of your User
class the expression json["route"]
will return something like { "routeID": "aaaaaaaa", "routeTitle": "bbbbbbbb" }
which is not a String
but rather a Map<String, dynamic>
which is why you are getting the error message above.
CodePudding user response:
Flutter Box.!
You just need to do simple job is to visit following website. And need to put your response or map/list that you want to convert in model class and rest will be done by server.
By the way, For your List<Map<String, dynamic> data's model class is,
// To parse this JSON data, do
//
// final routeModelClass = routeModelClassFromJson(jsonString);
import 'dart:convert';
List<RouteModelClass> routeModelClassFromJson(String str) => List<RouteModelClass>.from(json.decode(str).map((x) => RouteModelClass.fromJson(x)));
String routeModelClassToJson(List<RouteModelClass> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class RouteModelClass {
RouteModelClass({
this.route,
this.distributor,
this.visitDate,
});
Route route;
Distributor distributor;
dynamic visitDate;
factory RouteModelClass.fromJson(Map<String, dynamic> json) => RouteModelClass(
route: Route.fromJson(json["route"]),
distributor: Distributor.fromJson(json["distributor"]),
visitDate: json["visitDate"],
);
Map<String, dynamic> toJson() => {
"route": route.toJson(),
"distributor": distributor.toJson(),
"visitDate": visitDate,
};
}
class Distributor {
Distributor({
this.distributorId,
this.distributorName,
});
String distributorId;
String distributorName;
factory Distributor.fromJson(Map<String, dynamic> json) => Distributor(
distributorId: json["distributorID"],
distributorName: json["distributorName"],
);
Map<String, dynamic> toJson() => {
"distributorID": distributorId,
"distributorName": distributorName,
};
}
class Route {
Route({
this.routeId,
this.routeTitle,
});
String routeId;
String routeTitle;
factory Route.fromJson(Map<String, dynamic> json) => Route(
routeId: json["routeID"],
routeTitle: json["routeTitle"],
);
Map<String, dynamic> toJson() => {
"routeID": routeId,
"routeTitle": routeTitle,
};
}