diff --git a/lib/src/hive.dart b/lib/src/hive.dart index 3f106f107..865975f72 100644 --- a/lib/src/hive.dart +++ b/lib/src/hive.dart @@ -37,9 +37,10 @@ class Hive { /// ``` static void registerAdapter( String typeName, - T? Function(dynamic json) fromJson, - ) { - _typeRegistry.register(Isar.fastHash(typeName), fromJson); + T? Function(dynamic json) fromJson, [ + Type? type, + ]) { + _typeRegistry.register(Isar.fastHash(typeName), fromJson, type); } /// Get or open the box with [name] in the given [directory]. If no directory diff --git a/lib/src/impl/type_registry.dart b/lib/src/impl/type_registry.dart index c8e0d7e8b..80399cead 100644 --- a/lib/src/impl/type_registry.dart +++ b/lib/src/impl/type_registry.dart @@ -12,14 +12,18 @@ class _TypeRegistry { final Map> _registry = {}; final Map> _reverseRegistry = {..._builtinTypes}; - void register(int typeId, T? Function(dynamic json) fromJson) { + void register( + int typeId, + T? Function(dynamic json) fromJson, + Type? type, + ) { if (T == dynamic) { throw ArgumentError('Cannot register dynamic type.'); } final handler = _TypeHandler(typeId, fromJson); _registry[typeId] = handler; - _reverseRegistry[T] = handler; + _reverseRegistry[type ?? T] = handler; } T fromJson(int? typeId, dynamic json) {