dynamic_value 2.0.0-nullsafety.4 copy "dynamic_value: ^2.0.0-nullsafety.4" to clipboard
dynamic_value: ^2.0.0-nullsafety.4 copied to clipboard

outdated

The easy way to work with JSON data, safe access to nested data in the Map and safe type conversion.

example/example.dart

import '../lib/dynamic_value.dart';

void main() {
  final json = {
    "id": 1,
    "name": "User 1",
    "created": "2021-02-17 00:00:00",
    "groups": [
      {
        "id": "1",
        "name": "Group 1",
      },
    ],
  };

  DynamicValue.builders[Group] = (data) => Group.fromData(data);

  final value = DynamicValue(json);

  final int? userId = value['id'].toInt;
  final String? userName = value['name'].toStr;
  final DateTime? userCreatedAt = value['created'].toDateTime;
  final int? userGroupId = value['groups'][0]['id'].toInt;
  final Group? userFirstGroup = value['groups'][0].to<Group>();
  final List<Group>? userGroups = value['groups'].toList<Group>();

  print('User ID: $userId');
  print('User name: $userName');
  print('User created at: $userCreatedAt');
  print('User group ID: $userGroupId');
  print('User first group: $userFirstGroup');
  print('User groups: $userGroups');
}

class Group {
  final int? id;
  final String? name;

  Group({
    this.id,
    this.name,
  });

  factory Group.fromData(DynamicValue data) {
    return Group(
      id: data['id'].toInt,
      name: data['name'].toStr,
    );
  }

  @override
  String toString() {
    return '<Group #$id: $name>';
  }
}
8
likes
0
pub points
10%
popularity

Publisher

verified publisheramazingsoftworks.com

The easy way to work with JSON data, safe access to nested data in the Map and safe type conversion.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on dynamic_value