CreateCookie function
Creates a host-only cookie that is available throughout the application.
Values and names are URI-encoded so delimiter characters and Unicode are
stored safely. Passing zero for days removes the cookie.
Implementation
void CreateCookie(String key, String value, int days) {
_validateStorageKey(key);
if (days < 0) {
throw ArgumentError.value(days, 'days', 'Days cannot be negative.');
}
final encodedName = Uri.encodeComponent(key);
if (days == 0) {
_expireCookie(encodedName);
return;
}
// Remove cookies created by older versions with an explicit Domain before
// replacing them with a safer host-only cookie.
_expireCookie(encodedName);
final encodedValue = Uri.encodeComponent(value);
final cookie = _buildCookie(
encodedName,
encodedValue,
maxAgeSeconds: days * Duration.secondsPerDay,
);
if (cookie.length > _maxCookieSize) {
throw ArgumentError.value(
value,
'value',
'The encoded cookie is larger than $_maxCookieSize bytes.',
);
}
web.document.cookie = cookie;
}