User.fromJson constructor

User.fromJson(
  1. Map<String, dynamic> json
)

Creates a User instance from a JSON object.

This factory constructor is used to deserialize user data received from the Calljmp API. It handles the conversion of JSON data types to the appropriate Dart types.

Parameters

  • json: A Map containing the user data from the API

Returns

A new User instance populated with the JSON data

Example

final userData = {
  'id': 123,
  'email': 'user@example.com',
  'name': 'John Doe',
  'tags': ['role:member'],
  'createdAt': '2023-01-01T00:00:00Z',
};
final user = User.fromJson(userData);

Implementation

factory User.fromJson(Map<String, dynamic> json) {
  return User(
    id: json['id'] as int,
    email: json['email'] as String?,
    name: json['name'] as String?,
    avatar: json['avatar'] as String?,
    tags: (json['tags'] as List<dynamic>?)?.map((e) => e as String).toList(),
    createdAt: DateTime.parse(json['createdAt'] as String),
  );
}