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

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

Open
E-MRE opened this issue Dec 29, 2023 · 0 comments
Open

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

E-MRE opened this issue Dec 29, 2023 · 0 comments

Comments

@E-MRE
Copy link

E-MRE commented Dec 29, 2023

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

//open box
final box = Hive.box<List<SampleModel>>(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<SampleModel>>('List<SampleModel>', (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<String,dynamic> but got List".

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

For solution we can check json is List.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant