Session class

The session of one request: req.session. SessionMiddleware makes it.

The data is read on first use, so a request that never uses the session costs nothing. It is saved at the end of the request only when it changed.

req.session.put('cart', <int>[1, 2]);
List<dynamic>? cart = req.session.get('cart');
req.session.flash('message', 'Saved'); // the next request only

Values must be JSON types (Map, List, String, num, bool, null).

Constructors

Session({String? id, Object? read()?})
id is the id of a stored session (cache driver). read gives the stored session on first use, or null for a new one.

Properties

changed → bool
True when the request changed the session.
no setter
errors → Map<String, dynamic>
The flashed validation errors, like {'email': 'The email is taken'}. Empty when there are none.
no setter
hashCode → int
The hash code for this object.
no setterinherited
id ↔ String?
The session id for the cache driver. null until it is saved.
getter/setter pair
previousId ↔ String?
The id before regenerate, removed from the store on save.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
started → bool
True when the request used the session.
no setter
token → String
The CSRF token of the session. Made on first use.
no setter

Methods

all() → Map<String, dynamic>
All values. Changes to the map do not change the session.
flash(String key, Object? value) → void
Store value under key for this request and the next one that uses the session.
flashErrors(Object errors, {Map<String, dynamic>? input}) → void
Flash validation errors and the old input for the next request. Password fields (dontFlash) and uploaded files are not kept.
flush() → void
Remove all values, flash data too.
forget(String key) → void
Remove key.
get(String key, [Object? defaultValue]) → dynamic
The value of key, or defaultValue when it is missing.
has(String key) → bool
True when key has a value that is not null.
invalidate() → void
Remove all data and give the session a new id. Use it on logout.
keep(List<String> keys) → void
Keep the flash data of keys for one more request.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
old(String key, [Object? defaultValue]) → dynamic
The old input of key from the request before, see flashErrors.
pull(String key, [Object? defaultValue]) → dynamic
The value of key, removed from the session.
put(String key, Object? value) → void
Store value under key.
reflash() → void
Keep all flash data for one more request.
regenerate() → void
Give the session a new id and a new CSRF token, and keep the data. Call it after login, so an old id from before the login is no use.
toStore(Duration lifetime) → Map<String, dynamic>?
Internal, for SessionMiddleware: the session to store, or null when nothing changed. Flash data of the request before is removed here. A session older than half of lifetime is stored again, so it does not end while it is used.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

newId() → String
A new random session id.

Constants

dontFlash → const List<String>
Fields of a request that are never kept as old input.
errorsKey → const String
The key of the validation errors, see flashErrors.
oldInputKey → const String
The key of the old input, see flashErrors.