Session.fromMap constructor

Session.fromMap(
  1. Map<String, dynamic> map
)

Implementation

factory Session.fromMap(Map<String, dynamic> map) {
  final id = map['id'];
  if (id == null || id is! String) {
    throw ArgumentError('The `Session.id` is either empty or not a String.');
  }

  late final int? intExpires;
  final jsonExpires = map['expires'];
  switch (jsonExpires.runtimeType) {
    case String:
      try {
        intExpires = int.parse(jsonExpires as String);
      } catch (e, s) {
        print('$e\n$s');
        throw ArgumentError('The `Session.expires` value is not a valid integer.');
      }
      break;
    case int:
      intExpires = jsonExpires as int;
      break;
    default:
      throw ArgumentError('The `Session.expires` value has an unexpected type.');
  }
  final expires = DateTime.fromMicrosecondsSinceEpoch(intExpires);

  try {
    final data = map['data'] as Map<String, Object?>;
    return Session._(
      id: id,
      expires: expires,
      data: data,
    );
  } catch (e, s) {
    print('$e\n$s');
    throw ArgumentError('The `Session.data` is not in the expected format.');
  }
}