startSession method
Implementation
Future<void> startSession({
required String sessionId,
Map<String, Object?> metadata = const <String, Object?>{},
bool? clearPreviousOnChange,
}) async {
final clean = sessionId.trim();
if (clean.isEmpty) {
throw ArgumentError.value(sessionId, 'sessionId', 'Cannot be empty.');
}
final previousId = _activeSessionId;
final previousHash = _activeSessionHash;
final nextHash = _hash(clean);
final changed = previousHash != nextHash;
if (changed &&
(clearPreviousOnChange ?? config.clearPreviousSessionOnChange)) {
await _clearSessionHash(previousHash);
}
_activeSessionId = clean;
_activeSessionHash = nextHash;
if (metadata.isNotEmpty) {
await _box.put(
'__session__:$nextHash',
<String, dynamic>{
'sessionHash': nextHash,
'metadata': _normalizeForHive(metadata),
'updatedAtMs': DateTime.now().millisecondsSinceEpoch,
if (previousId != clean) 'previousSessionChanged': true,
},
);
}
}