-
Notifications
You must be signed in to change notification settings - Fork 195
Open
Description
The JSON returned by the server is sometimes non-standard, such as:
user.json
[{
"name": "",
"age": 0,
"discount": 0,
"friends": []
},{
"name": "",
"age": 0,
"discount": 0.0,
"friends": []
}]
Run:get generate model on home with assets/models/user.json
output:
class User {
String? name;
int? age;
double? discount;
List<Null>? friends;
User({this.name, this.age, this.discount, this.friends});
User.fromJson(Map<String, dynamic> json) {
name = json['name'];
age = json['age'];
discount = json['discount'];
if (json['friends'] != null) {
friends = <Null>[];
json['friends'].forEach((v) {
friends?.add(Null.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['name'] = name;
data['age'] = age;
data['discount'] = discount;
if (friends != null) {
data['friends'] = friends?.map((v) => v.toJson()).toList();
}
return data;
}
}
flutter run:
[Error: fluent/runtime/start_vm_initializer. cc (41)] Unhandled Exception: type 'int' is not a subtype of type 'double?'
I will double? Change to num? After solving this problem, how do I mention int when generating the model Double? Unified replacement with num? Type?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels