isar / isar/hive

[Hive v4] Type mistmatch when get List<T> items.

Open
#1,266 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

```
//register
Hive.registerAdapter('SampleModel', (json) => SampleModel.fromJson(json));

//open box
final box = Hive.box>(name: 'myBox');

//add items to specific key
box.put('list', const [SampleModel(id: 1, name: 'name'), SampleModel(id: 2, name: 'same')]);

//get items
final list = box.get('list');
```

When I try to get items from key fromJson method throws exception like "Type mismatch. Expected List but got List". Message comes from type_registry.dart class inside of the fromJson method.

If I add register adapter for list type like this

```
Hive.registerAdapter>('List', (json) {
if (json is List) {
return json.map((e) => SampleModel.fromJson(e)).toList();
}

debugPrint('json type: ${json.runtimeType}');
return [];
});
```

then exception message change "Type mismatch. Map but got List".

Exception throws because fromJson method doesn't support list type.

For solution we can check json is List.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in type_registry.dart and inspect the fromJson method, then reproduce the issue with the provided Hive.box> example and adapter registration. Done means retrieving the stored list no longer raises a type-mismatch exception for List.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.