Hive: Error with TypeAdapter

Created on 30 Sep 2019  路  8Comments  路  Source: hivedb/hive

This is my HiveHelper class

class HiveHelper {

  open() async {
    Directory appDocDir = await getApplicationDocumentsDirectory();
    String dbFilePath = [appDocDir.path, 'majesty_database'].join('/');
    Hive.init(dbFilePath);

    // Box For UserDetail
    var box = await Hive.openBox(Boxes.userDetail); // This is where code breaks
    box.registerAdapter(UserDetailAdapter(), 0);
  }

  close() {
    Hive.close();
  }
}

class Boxes {
  static final String userDetail = 'user_detail';
}

And in main.dart

void main() async {
  HiveHelper _hiveHelper = HiveHelper();
  await _hiveHelper.open();

  runApp(MajestyApp());
}

This code runs successfully on the first couple times. After maybe third or fourth time it breaks at the box opening in the open method in HiveHelper class and gives me the following error

E/flutter (20679): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HiveError: Cannot read, unknown typeId: 32. Did you forget to register an adapter?
E/flutter (20679): #0      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:243:9)
E/flutter (20679): #1      Frame.decodeValue (package:hive/src/binary/frame.dart:89:22)
E/flutter (20679): #2      Frame.decode (package:hive/src/binary/frame.dart:78:19)
E/flutter (20679): #3      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:77:25)
E/flutter (20679): <asynchronous suspension>
E/flutter (20679): #4      StorageBackendVm.initialize (package:hive/src/backend/storage_backend_vm.dart:85:25)
E/flutter (20679): <asynchronous suspension>
E/flutter (20679): #5      BoxBase.initialize (package:hive/src/box/box_base.dart:87:20)
E/flutter (20679): #6      HiveImpl.openBoxInternal (package:hive/src/hive_impl.dart:92:15)
E/flutter (20679): <asynchronous suspension>
E/flutter (20679): #7      HiveImpl.openBox (package:hive/src/hive_impl.dart:72:23)
E/flutter (20679): <asynchronous suspension>
E/flutter (20679): #8      HiveHelper.open (package:ninja_app/src/service/local_database/hive_helper.dart:24:26)
E/flutter (20679): <asynchronous suspension>
E/flutter (20679): #9      main (package:ninja_app/main.dart:43:21)
E/flutter (20679): #10     _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
E/flutter (20679): #11     main (package:ninja_app/main.dart:27:10)
E/flutter (20679): #12     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:229:25)
E/flutter (20679): #13     _rootRun (dart:async/zone.dart:1124:13)
E/flutter (20679): #14     _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (20679): #15     _runZoned (dart:async/zone.dart:1516:10)
E/flutter (20679): #16     runZoned (dart:async/zone.dart:1500:12)
E/flutter (20679): #17     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:221:5)
E/flutter (20679): #18     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:305:19)
E/flutter (20679): #19     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

It will also be great if you have simple flutter todo app example.

Version

  • Platform: Android
  • Flutter version: v1.9.1+hotfix.2
  • Hive version: 1.0.0
documentation

Most helpful comment

Sorry there is an error in the documentation. You need to use Hive.registerAdapter() before you open the box.

All 8 comments

Sorry there is an error in the documentation. You need to use Hive.registerAdapter() before you open the box.

I believe that Hive.registerAdapter() will register this Adapter for all the boxes. I want this adapter registered only to UserDetailBox, that is why I used box.registerAdapter() or is this functionality removed?

This only workes for lazy boxes and the documentation fails to mention that.

I will deprecate and remove box.registerAdapter() in the coming versions.

There is nothing wrong with registering an adapter for all boxes. Or do you need to store the same object in multiple boxes with different adapters?

Atm my use case is very simple(just a local cache of current user detail), so I don't need multiple adapters. I'll use the Hive.registerAdapter() method then. I believe there won't be any performance degradation by registering globally?

Also thank you for creating this awesome library, much appreciated.

I believe there won't be any performance degradation by registering globally?

Not at all. They are handled the same way.

Sorry there is an error in the documentation. You need to use Hive.registerAdapter() before you open the box.

this solved issue for me. thanks!

belgelerde bir hata v

this worked for me. Thank you.

How can I save the same object on multiple boxes? Im getting this error: The same instance of an HiveObject cannot be stored in two different boxes.

Was this page helpful?
0 / 5 - 0 ratings