ClientCookie.fromSetCookie constructor Null safety

ClientCookie.fromSetCookie(
  1. String cookieItem
)

Parses one 'set-cookie' item

Implementation

factory ClientCookie.fromSetCookie(String cookieItem) {
  final List<String> parts =
      cookieItem.split(';').reversed.map((String str) => str.trim()).toList();

  if (parts.isEmpty) throw Exception('Invalid cookie set!');

  String name;
  String value;
  final map = {};

  {
    final String first = parts.removeLast();
    final int idx = first.indexOf('=');
    if (idx == -1) throw Exception('Invalid Name=Value pair!');
    name = first.substring(0, idx).trim();
    value = first.substring(idx + 1).trim();
    if (name.isEmpty) {
      throw Exception('Cookie must have a name!');
    }
  }

  for (String directive in parts) {
    final List<String> points =
        directive.split('=').map((String str) => str.trim()).toList();
    if (points.length == 0 || points.length > 2)
      throw Exception('Invalid directive!');
    final String key = points.first;
    final String val = points.length == 2 ? points.last : '';
    if (!_parsers.containsKey(key)) {
      throw _invalidDirective;
    }
    map[key] = _parsers[key](val);
  }

  return ClientCookie.fromMap(name, value, DateTime.now(), map);
}