Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add Type parameter to resolve bug with generic types in mixin usage #1310

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/src/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class Hive {
/// ```
static void registerAdapter<T>(
String typeName,
T? Function(dynamic json) fromJson,
) {
_typeRegistry.register<T>(Isar.fastHash(typeName), fromJson);
T? Function(dynamic json) fromJson, [
Type? type,
]) {
_typeRegistry.register<T>(Isar.fastHash(typeName), fromJson, type);
}

/// Get or open the box with [name] in the given [directory]. If no directory
Expand Down
8 changes: 6 additions & 2 deletions lib/src/impl/type_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ class _TypeRegistry {
final Map<int, _TypeHandler<dynamic>> _registry = {};
final Map<Type, _TypeHandler<dynamic>> _reverseRegistry = {..._builtinTypes};

void register<T>(int typeId, T? Function(dynamic json) fromJson) {
void register<T>(
int typeId,
T? Function(dynamic json) fromJson,
Type? type,
) {
if (T == dynamic) {
throw ArgumentError('Cannot register dynamic type.');
}

final handler = _TypeHandler<T>(typeId, fromJson);
_registry[typeId] = handler;
_reverseRegistry[T] = handler;
_reverseRegistry[type ?? T] = handler;
}

T fromJson<T>(int? typeId, dynamic json) {
Expand Down
Loading