get method
Implementation
@override
Future<dynamic> get(String key) async {
final database = await _database;
final transaction = database.transaction(_recordStore, idbModeReadOnly);
final raw = await transaction
.objectStore(_recordStore)
.getObject(_recordId(key));
await transaction.completed;
if (raw != null) {
if (raw is! String) {
throw const FormatException('IndexedDB record is not a string');
}
return codec.decode(raw, expectedKey: key, allowLegacy: false).value;
}
if (namespace != 'default' || !_hasLegacyStore) return null;
final legacyTransaction = database.transaction(
_legacyStore,
idbModeReadOnly,
);
final legacyRaw = await legacyTransaction
.objectStore(_legacyStore)
.getObject(key);
await legacyTransaction.completed;
if (legacyRaw == null) return null;
if (legacyRaw is! String) {
throw const FormatException('Legacy IndexedDB record is not a string');
}
final decoded = codec.decode(
legacyRaw,
expectedKey: key,
allowLegacy: true,
legacyFallbackKey: key,
);
final migration = database.transactionList([
_recordStore,
_legacyStore,
], idbModeReadWrite);
await migration
.objectStore(_recordStore)
.put(codec.encode(key, decoded.value), _recordId(key));
await migration.objectStore(_legacyStore).delete(key);
await migration.completed;
notifyListeners(key);
return decoded.value;
}