From 79b14882306c114b15c67522df1e3558a23521f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Ayd=C4=B1n?= <84200491+ahmtydn@users.noreply.github.com> Date: Sun, 15 Sep 2024 07:30:48 +0300 Subject: [PATCH 1/2] Fix: Add Type parameter to resolve bug with generic types in mixin usage - Added Type parameter in adapter registration to fix issues with generic types when using mixins. - This change addresses the bug where mixin-based models were not correctly recognized by the type registry. --- lib/src/hive.dart | 3 ++- lib/src/impl/type_registry.dart | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/src/hive.dart b/lib/src/hive.dart index 3f106f107..1361f4bdf 100644 --- a/lib/src/hive.dart +++ b/lib/src/hive.dart @@ -38,8 +38,9 @@ class Hive { static void registerAdapter( String typeName, T? Function(dynamic json) fromJson, + Type? type, ) { - _typeRegistry.register(Isar.fastHash(typeName), fromJson); + _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) { From 5eb6fe59fdb70d332a27102ecccd4efc0bfde7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Ayd=C4=B1n?= <84200491+ahmtydn@users.noreply.github.com> Date: Sun, 15 Sep 2024 07:39:57 +0300 Subject: [PATCH 2/2] set as optional parameter --- lib/src/hive.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/hive.dart b/lib/src/hive.dart index 1361f4bdf..865975f72 100644 --- a/lib/src/hive.dart +++ b/lib/src/hive.dart @@ -37,9 +37,9 @@ class Hive { /// ``` static void registerAdapter( String typeName, - T? Function(dynamic json) fromJson, + T? Function(dynamic json) fromJson, [ Type? type, - ) { + ]) { _typeRegistry.register(Isar.fastHash(typeName), fromJson, type); }