isar / isar/hive

Unhandled Exception: type 'Account' is not a subtype of type 'Order' of 'obj'

Open
#568 9 comments 1 reaction 1 assignee Claimed by @simc View on GitHub
problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

I got this error when I try to save an `Account` to a box:
```
E/flutter (19165): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type 'Account' is not a subtype of type 'Order' of 'obj'
E/flutter (19165): #0 OrderAdapter.write (package:distribuidora_griwert_distributors/application/data_source/adapters/order_adapter.dart)
package:distribuidora_griwert_distributors/…/adapters/order_adapter.dart:1
E/flutter (19165): #1 BinaryWriterImpl.write
package:hive/…/binary/binary_writer_impl.dart:316
E/flutter (19165): #2 BinaryWriterImpl.writeFrame
package:hive/…/binary/binary_writer_impl.dart:254
E/flutter (19165): #3 StorageBackendVm.writeFrames.
package:hive/…/vm/storage_backend_vm.dart:123
E/flutter (19165): #4 ReadWriteSync.syncWrite.
package:hive/…/vm/read_write_sync.dart:26
```
It seems like Hive is getting the wrong adapter. `Account`and `Order` are classes that have their corresponding adapter.
They are registered in the following order: `OrderAdapter(), AccountAdapter()`. If I invert the order, the exception is the same when I try to save an Order type.

I have a wrapper around Hive.registerAdapter to register them like:
```dart
await Storage.instance.init(options: StorageOptions(
adapters: [OrderAdapter(), AccountAdapter()]
));
```

And then:
```dart
if(options != null) {

// Register Hive adapters
if(options.adapters != null && options.adapters.length > 0) {
options.adapters.forEach((adapter) => Hive.registerAdapter(adapter));
}
}

Hive.init(getAppDataPath("hive"));
```

Adapters are registered before `Hive.init()`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.