Last updated:
0 purchases
flutter annotation json
flutter_annotation_json #
JSON to Model based on annotations, type safe.
Example #
import 'package:flutter_annotation_json/flutter_annotation_json.dart';
part 'user.json.dart';
@JsonObject()
class UserInfo {
final String name;
final int age;
final double height;
final bool isVip;
final DateTime lastLogin;
final List<String> photos;
final UserLevel level;
final String? bio;
final AddressInfo? addressInfo;
UserInfo({required this.name,
required this.age,
required this.height,
required this.isVip,
required this.lastLogin,
required this.photos,
required this.level,
this.bio,
this.addressInfo,
required this.session});
factory UserInfo.fromJson(Map<String, dynamic> json) =>
_$UserInfoFromJson(json);
}
@JsonObject()
class AddressInfo {
final String province;
final String city;
final String area;
AddressInfo({required this.province, required this.city, required this.area});
factory AddressInfo.fromJson(Map<String, dynamic> json) =>
_$AddressInfoFromJson(json);
}
copied to clipboard
Optional Method
toJson
Map<String, dynamic> toJson() => _$AddressInfoToJson(this);
copied to clipboard
copyWith
AddressInfo copyWith({String? province, String? city, String? area}) =>
_$AddressInfoCopyWith(this,
province: province, city: city, area: area
);
copied to clipboard
Use Enum
part 'user.enum.dart';
@JsonEnum('vip')
enum UserLevel {
@EnumValue('normal')
normal,
@EnumValue('vip')
vip,
@EnumValue('svip')
svip
}
extension UserLevelValues on UserLevel {
String get value => _$getValueForUserLevel(this);
}
copied to clipboard
Custom Encode&Decode #
dynamic durationEncode(dynamic value) {
return (value as Duration).inMilliseconds;
}
Duration durationDecode(dynamic value) {
return Duration(milliseconds: value);
}
@JsonField(encoder: durationEncode, decoder: durationDecode)
final Duration session;
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.