TimeId.fromJson constructor

TimeId.fromJson(
  1. Map<String, dynamic> map, {
  2. String? key,
})

Returns a valid TimeId object.

If key is null TimeId.key is used to get it's corresponding value in map.

Throws FormatException if DateTime.parse fails to parse string.

Implementation

factory TimeId.fromJson(Map<String, dynamic> map, {String? key}) {
  final flatMap = flattenMap(map);
  final value = flatMap[key ?? TimeId.key].toString();

  final idSplit = value.split('*');

  String? randStr;
  try {
    randStr = idSplit[1];
  } catch (_) {}

  return TimeId.withTime(
    DateTime.parse(idSplit[0]),
    randStr: randStr,
  );
}