diff --git a/packages/hydrated_bloc/lib/src/hydrated_storage.dart b/packages/hydrated_bloc/lib/src/hydrated_storage.dart index 7ef30640f57..98f59f12e1c 100644 --- a/packages/hydrated_bloc/lib/src/hydrated_storage.dart +++ b/packages/hydrated_bloc/lib/src/hydrated_storage.dart @@ -85,7 +85,6 @@ class HydratedStorage implements Storage { HydratedCipher? encryptionCipher, }) { return _lock.synchronized(() async { - if (_instance != null) return _instance!; // Use HiveImpl directly to avoid conflicts with existing Hive.init // https://github.com/hivedb/hive/issues/336 hive = HiveImpl(); @@ -105,7 +104,7 @@ class HydratedStorage implements Storage { await _migrate(storageDirectory, box); } - return _instance = HydratedStorage(box); + return HydratedStorage(box); }); } @@ -133,7 +132,6 @@ class HydratedStorage implements Storage { static late HiveInterface hive; static final _lock = Lock(); - static HydratedStorage? _instance; final Box _box; @@ -157,7 +155,6 @@ class HydratedStorage implements Storage { @override Future clear() async { if (_box.isOpen) { - _instance = null; return _lock.synchronized(_box.clear); } } @@ -165,7 +162,6 @@ class HydratedStorage implements Storage { @override Future close() async { if (_box.isOpen) { - _instance = null; return _lock.synchronized(_box.close); } } diff --git a/packages/hydrated_bloc/new/hydrated_box.hive b/packages/hydrated_bloc/new/hydrated_box.hive new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/hydrated_bloc/test/hydrated_storage_test.dart b/packages/hydrated_bloc/test/hydrated_storage_test.dart index 84c006bd7e5..26d7d14e485 100644 --- a/packages/hydrated_bloc/test/hydrated_storage_test.dart +++ b/packages/hydrated_bloc/test/hydrated_storage_test.dart @@ -50,14 +50,19 @@ void main() { await storage.close(); }); - test('reuses existing instance when called multiple times', () async { + test('override existing instance when called multiple times', () async { final instanceA = storage = await HydratedStorage.build( - storageDirectory: storageDirectory, + storageDirectory: + await Directory(storageDirectory.path + "/new").create(), ); + final instanceB = await HydratedStorage.build( storageDirectory: storageDirectory, ); - expect(instanceA, instanceB); + + await instanceA.close(); + await instanceB.close(); + expect(instanceA, isNot(instanceB)); }); test('creates new instance if storage was closed', () async { @@ -191,12 +196,13 @@ void main() { // ignore: unawaited_futures storage.write(token, record); - + await storage.close(); + await storage.clear(); storage = await HydratedStorage.build( storageDirectory: Directory(cwd), ); - final written = storage.read(token) as List>; + final written = storage.read(token) as List; expect(written, isNotNull); expect(written, record); }).drain();