Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
- Allow calling `.init()` multiple times, instead of throwing error Hive will print warning to console
- Hive will warn developers when registering adapters for `dynamic` type
  • Loading branch information
themisir committed Mar 28, 2021
1 parent 2af7d77 commit 50e4b71
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions hive/lib/src/hive_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions hive/lib/src/registry/type_registry_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyType>(MyTypeAdapter())',
);
}
var typeId = adapter.typeId;
if (!internal) {
if (typeId < 0 || typeId > 223) {
Expand Down
5 changes: 5 additions & 0 deletions hive/test/tests/registry/type_registry_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void main() {
expect(() => registry.registerAdapter(TestAdapter()),
throwsHiveError('already a TypeAdapter for typeId'));
});

test('dynamic type', () {
var registry = TypeRegistryImpl();
registry.registerAdapter<dynamic>(TestAdapter());
});
});

test('.findAdapterForTypeId()', () {
Expand Down
6 changes: 1 addition & 5 deletions hive_flutter/lib/src/hive_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

0 comments on commit 50e4b71

Please sign in to comment.