toCookieString method

Future<String?> toCookieString()

Implementation

Future<String?> toCookieString() async {
  await _ensureOpen();
  if (_cookies.isNotEmpty) {
    final result = StringBuffer();
    final now = DateTime.now();
    final remove = <Cookie>[];
    for (final cookie in _cookies) {
      if (now.isAfter(cookie.expires)) {
        remove.add(cookie);
        continue;
      }
      if (result.isNotEmpty) {
        result.write("; ");
      }
      result.write("${cookie.name}=${cookie.value}");
    }
    _cookies.removeAll(remove);
    final resultStr = result.toString();
    _log("Request Cookies: $resultStr");
    return resultStr;
  } else {
    return null;
  }
}