diff --git a/hive/lib/src/hive_impl.dart b/hive/lib/src/hive_impl.dart index ecf52a541..bbbde0c66 100644 --- a/hive/lib/src/hive_impl.dart +++ b/hive/lib/src/hive_impl.dart @@ -55,8 +55,11 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface { @override void init(String path) { - if (homePath != null) { - throw HiveError('Instance has already been initialized.'); + if (homePath == path) { + print( + 'Hive instance has already been initialized. Please call Hive.init ' + 'only once if possible.', + ); } homePath = path; } diff --git a/hive/lib/src/registry/type_registry_impl.dart b/hive/lib/src/registry/type_registry_impl.dart index a66cd752d..56ef4de61 100644 --- a/hive/lib/src/registry/type_registry_impl.dart +++ b/hive/lib/src/registry/type_registry_impl.dart @@ -73,6 +73,16 @@ class TypeRegistryImpl implements TypeRegistry { bool internal = false, bool override = false, }) { + if (T == dynamic || T == Object) { + print( + 'Registering type adapters for dynamic type is must be avoided, ' + 'otherwise all the write requests to Hive will be handled by given ' + 'adapter. Please explicitly provide adapter type on registerAdapter ' + 'method to avoid this kind of issues. For example if you want to ' + 'register MyTypeAdapter for MyType class you can call like this: ' + 'registerAdapter(MyTypeAdapter())', + ); + } var typeId = adapter.typeId; if (!internal) { if (typeId < 0 || typeId > 223) { diff --git a/hive/test/tests/registry/type_registry_impl_test.dart b/hive/test/tests/registry/type_registry_impl_test.dart index 0f1dafad4..8bd81ce85 100644 --- a/hive/test/tests/registry/type_registry_impl_test.dart +++ b/hive/test/tests/registry/type_registry_impl_test.dart @@ -60,6 +60,11 @@ void main() { expect(() => registry.registerAdapter(TestAdapter()), throwsHiveError('already a TypeAdapter for typeId')); }); + + test('dynamic type', () { + var registry = TypeRegistryImpl(); + registry.registerAdapter(TestAdapter()); + }); }); test('.findAdapterForTypeId()', () { diff --git a/hive_flutter/lib/src/hive_extensions.dart b/hive_flutter/lib/src/hive_extensions.dart index 26cc660a4..650b17f70 100644 --- a/hive_flutter/lib/src/hive_extensions.dart +++ b/hive_flutter/lib/src/hive_extensions.dart @@ -9,11 +9,7 @@ extension HiveX on HiveInterface { WidgetsFlutterBinding.ensureInitialized(); if (!kIsWeb) { var appDir = await getApplicationDocumentsDirectory(); - if (appDir != null) { - init(path_helper.join(appDir.path, subDir)); - } else { - NullThrownError(); - } + init(path_helper.join(appDir.path, subDir)); } } }