storeAll method

Future<void> storeAll()

Implementation

Future<void> storeAll() async {
  final file = await _ensureOpen();
  final sink = file.openWrite(mode: FileMode.write);
  _log("storing into $file");
  try {
    final now = DateTime.now();
    final remove = <Cookie>[];
    for (final cookie in _cookies) {
      if (now.isBefore(cookie.expires)) {
        _log("storing cookie: ${cookie.name}=${cookie.value};${cookie.expires.toIso8601String()}");
        sink
          ..write(cookie.name)
          ..write("=")
          ..write(cookie.value)
          ..write(";")
          ..write(cookie.expires.toIso8601String())
          ..write("\n");
      } else {
        remove.add(cookie);
      }
    }
    _log("removing expired cookies: $remove");
    _cookies.removeAll(remove);
  } finally {
    await sink.flush();
    await sink.close();
  }
}