toStore method

Map<String, dynamic>? toStore(
  1. Duration lifetime
)

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.

Implementation

Map<String, dynamic>? toStore(Duration lifetime) {
  Map<String, dynamic>? data = _data;
  if (data == null) return null;
  if (_flashOld.isNotEmpty) {
    for (String key in _flashOld) {
      if (!_flashNew.contains(key)) data.remove(key);
    }
    _flashOld = <String>[];
    _changed = true;
  }
  int now = DateTime.now().millisecondsSinceEpoch;
  if (now - _savedAt > lifetime.inMilliseconds ~/ 2) _changed = true;
  if (!_changed) return null;
  _savedAt = now;
  return <String, dynamic>{
    'data': data,
    'flash': _flashNew,
    'token': _token,
    'at': now,
  };
}