cookie<T> method
Gets a cookie by name and casts it to type T
.
If the cookie doesn't exist, it returns null
.
If T
is String, it's casted to a string.
If T
is bool, it's parsed from a string.
If T
is int, it's parsed from a string.
If T
is double, it's parsed from a string.
Otherwise, it's casted to T
.
Implementation
T? cookie<T>(String key) {
if (_cookies[key] == null) return null;
return switch (T.toString()) {
'String' => _cookies[key].toString(),
'bool' => bool.parse(_cookies[key]) as T,
'int' => int.parse(_cookies[key]) as T,
'double' => double.parse(_cookies[key]) as T,
(_) => _cookies[key],
};
}