diff --git a/hive/lib/src/box_collection/box_collection.dart b/hive/lib/src/box_collection/box_collection.dart index 55f6d746..36bc69cb 100644 --- a/hive/lib/src/box_collection/box_collection.dart +++ b/hive/lib/src/box_collection/box_collection.dart @@ -18,8 +18,6 @@ class BoxCollection implements implementation.BoxCollection { /// TODO: Document this! BoxCollection(this.name, this.boxNames); - static bool _hiveInit = false; - late Box _badKeyBox; /// TODO: Document this! @@ -29,9 +27,8 @@ class BoxCollection implements implementation.BoxCollection { String? path, HiveCipher? key, }) async { - if (!_hiveInit) { + if (!Hive.isInitialized) { Hive.init(path ?? './'); - _hiveInit = true; } final collection = BoxCollection(name, boxNames); if (key != null) { diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart index 2ac0d2ec..3d5d82b1 100644 --- a/hive/lib/src/hive.dart +++ b/hive/lib/src/hive.dart @@ -11,6 +11,9 @@ abstract class HiveInterface implements TypeRegistry { HiveStorageBackendPreference.native, }); + /// Returns true if Hive is initialized. + bool get isInitialized; + /// Opens a box. /// /// If the box is already open, the instance is returned and all provided diff --git a/hive/lib/src/hive_impl.dart b/hive/lib/src/hive_impl.dart index 12dd3c37..141c1a12 100644 --- a/hive/lib/src/hive_impl.dart +++ b/hive/lib/src/hive_impl.dart @@ -33,6 +33,9 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface { @visibleForTesting String? homePath; + @override + bool get isInitialized => homePath != null; + /// Not part of public API HiveImpl() { _registerDefaultAdapters(); diff --git a/hive/test/tests/box_collection/box_collection_test.dart b/hive/test/tests/box_collection/box_collection_test.dart index 4ecbcdbd..0db648b1 100644 --- a/hive/test/tests/box_collection/box_collection_test.dart +++ b/hive/test/tests/box_collection/box_collection_test.dart @@ -1,4 +1,5 @@ -import 'package:hive_ce/src/box_collection/box_collection.dart'; +import 'package:hive_ce/hive.dart'; +import 'package:hive_ce/src/hive_impl.dart'; import 'package:test/test.dart'; Future _openCollection({bool withData = false}) async { @@ -116,5 +117,11 @@ void main() { await box.clear(); expect(await box.getAllKeys(), []); }); + + test('Does not reinitialize Hive path', () async { + Hive.init('MYPATH'); + await _openCollection(); + expect((Hive as HiveImpl).homePath, 'MYPATH'); + }); }); }