flutter_annotation_json 0.0.1 copy "flutter_annotation_json: ^0.0.1" to clipboard
flutter_annotation_json: ^0.0.1 copied to clipboard

JSON to Model based on annotations, type safe.

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);
}

Optional Method

  • toJson
Map<String, dynamic> toJson() => _$AddressInfoToJson(this);
  • copyWith
AddressInfo copyWith({String? province, String? city, String? area}) =>
    _$AddressInfoCopyWith(this,
        province: province, city: city, area: area
    );

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);
}

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;
0
likes
130
points
50
downloads

Publisher

unverified uploader

Weekly Downloads

JSON to Model based on annotations, type safe.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on flutter_annotation_json