diff --git a/lib/src/database/indexeddb_box.dart b/lib/src/database/indexeddb_box.dart index a804e1a0..ba04971b 100644 --- a/lib/src/database/indexeddb_box.dart +++ b/lib/src/database/indexeddb_box.dart @@ -166,7 +166,10 @@ class Box { txn ??= boxCollection._db.transaction(name, 'readwrite'); final store = txn.objectStore(name); await store.delete(key); - _cache.remove(key); + + // Set to null instead remove() so that inside of transactions null is + // returned. + _cache[key] = null; _cachedKeys?.remove(key); return; } @@ -183,7 +186,7 @@ class Box { final store = txn.objectStore(name); for (final key in keys) { await store.delete(key); - _cache.remove(key); + _cache[key] = null; _cachedKeys?.remove(key); } return; diff --git a/lib/src/database/sqflite_box.dart b/lib/src/database/sqflite_box.dart index 16e1a9ca..35c9bcec 100644 --- a/lib/src/database/sqflite_box.dart +++ b/lib/src/database/sqflite_box.dart @@ -302,7 +302,9 @@ class Box { txn.delete(name, where: 'k = ?', whereArgs: [key]); } - _cache.remove(key); + // Set to null instead remove() so that inside of transactions null is + // returned. + _cache[key] = null; _cachedKeys?.remove(key); return; } @@ -326,7 +328,7 @@ class Box { } for (final key in keys) { - _cache.remove(key); + _cache[key] = null; _cachedKeys?.removeAll(keys); } return; diff --git a/test/box_test.dart b/test/box_test.dart index 2ab4ec3b..5b66514d 100644 --- a/test/box_test.dart +++ b/test/box_test.dart @@ -59,6 +59,18 @@ void main() { await box.clear(); }); + test('Box.delete in transaction', () async { + final box = collection.openBox('cats'); + await box.put('fluffy', data); + await box.put('loki', data2); + await collection.transaction(() async { + await box.delete('fluffy'); + expect(await box.get('fluffy'), null); + }); + expect(await box.get('fluffy'), null); + await box.clear(); + }); + test('Box.deleteAll', () async { final box = collection.openBox('cats'); await box.put('fluffy', data);