UserProfileFromJson function

UserProfile UserProfileFromJson(
  1. Map<String, dynamic> json
)

Implementation

UserProfile UserProfileFromJson(Map<String, dynamic> json) {
  if (json['kind'] != 'user_profile') {
    throw ArgumentError(
      'Invalid discriminator for UserProfile at kind: expected user_profile',
    );
  }
  if (!json.containsKey('id') || json['id'] == null) {
    throw ArgumentError('Missing required field UserProfile.id (id)');
  }
  if (!json.containsKey('fullName') || json['fullName'] == null) {
    throw ArgumentError(
      'Missing required field UserProfile.fullName (fullName)',
    );
  }
  const Set<String> _allowedKeys = <String>{
    'kind',
    'id',
    'fullName',
    'active',
    'status',
    'birthDate',
    'createdAt',
    'tags',
    'scores',
    'metadata',
    'address',
    'balance',
  };
  for (final String key in json.keys) {
    if (!_allowedKeys.contains(key)) {
      throw ArgumentError('Unknown field for UserProfile: $key');
    }
  }
  return UserProfile(
    id: (json['id'] as num).toInt(),
    fullName: ((json['fullName'] as String) as String).trim(),
    active: json['active'] == null ? true : json['active'] as bool,
    status: UserStatus.values.firstWhere(
      (e) => e.name == (json['status'] as String),
      orElse: () => UserStatus.values.byName('unknown'),
    ),
    birthDate: Serializer.parseDate(
      ((DateTime.parse(json['birthDate'] as String)) as String),
      'yyyy-MM-dd',
    ),
    createdAt: Serializer.parseDate(
      ((DateTime.parse(json['createdAt'] as String)) as String),
      'iso8601',
    ),
    tags: (json['tags'] as List).map((e) => e as String).toList(),
    scores: ((json['scores'] as List).map((e) => (e as num).toInt())).toSet(),
    metadata: (json['metadata'] as Map).map(
      (k, v) => MapEntry(k.toString(), v as String),
    ),
    address: AddressFromJson(json['address'] as Map<String, dynamic>),
    balance: MoneyFromJson(json['balance']),
  );
}