我使用Freezed包来生成我的类。我有类和生成toJson & fromJson,我有问题从API到Object生成json。我从API返回json如下:
{
"status": "ok",
"message": "Data utang sebagai pembertang ",
"data": [
{
"id_utang": "124",
"pembertang": {
"id_user": "111395227810958186033",
"name_user": "Zeffry Reynando",
"email_user": "[email protected]",
"image_user": "https://lh3.googleusercontent.com/a-/AOh14GiIqxTHoBba1eooOh9TmsBkVFb9GQzz3zByx8IIKA=s96-c",
"token_fcm": "dk5s7T9ARrmSVHl1e4T1zc:APA91bFUEyOcwjLCZEuW525bxn9forOPVxvc2IGlc2fD90rf0znghnuGtJWuWnaGIs_1Zw8xBfuJ5B5ggj1zut4fenzJHqD7PgCrHbiHBu0vFqcyVZw9zHfiYDN9kBSUj2ekouIu99Su",
"created_date": "2020-07-26 12:04:33"
},
"pengutang": {
"id_user": "105164526876912904397",
"name_user": "Akun Testing",
"email_user": "akun.testi[email protected]",
"image_user": "https://lh5.googleusercontent.com/-p46wtaUFEQA/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucn5itDrBrYoZfLoCRfqHzkLo-FgIA/s96-c/photo.jpg",
"token_fcm": "eqzBVVRAQOqRBKdyi_oQHk:APA91bHOXO0DKTkDVplUte_-itvcPNohcpRCIYFmgSgGn6o9xobk6kkoiibIWP_v5uFYe7EXHyH0NAJvzQIB9XSViQGYn7OIGgW26hzAY4-H3jhbCyB0t9bCNx2WzYF9nT5vFiYutBfb",
"created_date": "2020-07-25 14:10:46"
},
"total_utang": "150000",
"sisa_utang": "150000",
"tgl_kembali": "2020-10-10 00:00:00",
"status": "1",
"keterangan": "kepo lu",
"selfie": "3ed425415636e023107b27b4f2d33844.jpg",
"ttd": "askdj ksajd lasjdlksajd jsadkjsakd",
"created_date": "2020-07-26 09:25:56"
},
]
}
但是当我用final listUtang = list.map((e) => UtangModel.fromJson(e)).toList();
生成那个json时。我在上面返回null。
List After Compiled UtangModel(idUtang: null, pembertang: UserGoogleModel(idUser: null, nameUser: null, emailUser: null, imageUser: null, tokenFcm: null, createdDate: null), pengutang: UserGoogleModel(idUser: null, nameUser: null, emailUser: null, imageUser: null, tokenFcm: null, createdDate: null), totalUtang: null, sisaUtang: null, tglKembali: null, status: 1, keterangan: kepo lu, selfie: 3ed425415636e023107b27b4f2d33844.jpg, ttd: askdj ksajd lasjdlksajd jsadkjsakd, createdDate: null)
在上面的result json中,它肯定有数据,但用fromJson生成后返回null。我错过什么了吗?
UtangModel
@freezed
abstract class UtangModel with _$UtangModel {
const factory UtangModel(
{String idUtang,
UserGoogleModel pembertang,
UserGoogleModel pengutang,
String totalUtang,
String sisaUtang,
DateTime tglKembali,
String status,
String keterangan,
String selfie,
String ttd,
DateTime createdDate}) = _UtangModel;
factory UtangModel.fromJson(Map<String, dynamic> json) => _$UtangModelFromJson(json);
}
UserModel
@freezed
abstract class UserGoogleModel with _$UserGoogleModel {
const factory UserGoogleModel(
{String idUser,
String nameUser,
String emailUser,
String imageUser,
String tokenFcm,
DateTime createdDate}) = _UserGoogleModel;
factory UserGoogleModel.fromJson(Map<String, dynamic> json) => _$UserGoogleModelFromJson(json);
}
Pubspec.yaml
dependencies:
flutter:
sdk: flutter
freezed_annotation: ^0.11.0
json_annotation: ^3.0.1
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.9.0
flutter_launcher_icons: ^0.7.5
build_runner: ^1.10.0
freezed: ^0.11.4
json_serializable: ^3.3.0
2条答案
按热度按时间cu6pst1q1#
问题是JSON字段和类字段之间的区别。
UtangModel
类中有idUtang
字段,但JSON数据包含id_utang
字符串。这是一个不同的字符串,这就是为什么这里有null
-s。vsmadaxz2#
我有点晚了这个答案,但是的,它可能会帮助别人的
从ChessMax的答案来看,问题在于你的JsonKey名称,它在API响应上是不同的,而冻结创建JsonKey名称是其他的东西,所以要解决这个问题,你必须在你的主Model类上注解JsonKey。给前任的。
这里
@JsonKey(name: 'id_utang') String idUtang
是解此外,为Model类上的每个值注解JsonKey确实需要时间,因此您可以使用插件(如果您使用的是Android Studio),它可以通过注解JsonKey自动为您生成模型类
插件-https://plugins.jetbrains.com/plugin/16971-dartjsongenerator