Interval.fromJson constructor

Interval.fromJson(
  1. Iterable data
)

Creates an interval from data containing two String? items (start, end).

Start and end instants must be formatted according to RFC 3339. See DateTime.parse for reference of valid inputs for start and end parts.

Either start or end is considered open, if an item is null, an empty string, or a string with the value "..".

Throws FormatException if an interval cannot be parsed.

Implementation

factory Interval.fromJson(Iterable<dynamic> data) {
  if (data.length == 2) {
    final start = data.elementAt(0);
    final end = data.elementAt(1);
    if ((start is String?) && (end is String?)) {
      if (start == null || start.isEmpty || start == '..') {
        if (end == null || end.isEmpty || end == '..') {
          return const Interval.open();
        } else {
          return Interval.openStart(DateTime.parse(end));
        }
      } else if (end == null || end.isEmpty || end == '..') {
        return Interval.openEnd(DateTime.parse(start));
      } else {
        return Interval.closed(
          DateTime.parse(start),
          DateTime.parse(end),
        );
      }
    }
  }
  throw const FormatException('Cannot parse interval.');
}