Hive: Can someone please explain me the use of typeId?

Created on 4 Aug 2020  路  15Comments  路  Source: hivedb/hive

question

Most helpful comment

The database on your device contains a typeId which it does not know how to parse (because you changed it).

Please clear the storage or delete the database file and you will be fine.

All 15 comments

@leisim So, when do I increment the typeId? When I make changes in the schema?

I'm sorry, I'm new to this! Please guide.

You never change a typeId (you can still change your schema, just leave the typeId). You can add new ones for new types.

Oh, got it! So I can't have two HiveObjects with the same typeId?

I'm getting this error.

Unhandled Exception: HiveError: Cannot read, unknown typeId: 33. Did you forget to register an adapter?
E/flutter (  752): #0      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:322:11)
E/flutter (  752): #1      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:273:26)
E/flutter (  752): #2      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:17:26)
E/flutter (  752): #3      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:41:12)
E/flutter (  752): <asynchronous suspension>
E/flutter (  752): #4      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:82:30)
E/flutter (  752): <asynchronous suspension>
E/flutter (  752): #5      BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:90:20)
E/flutter (  752): #6      HiveImpl._openBox (package:hive/src/hive_impl.dart:90:17)
E/flutter (  752): <asynchronous suspension>
E/flutter (  752): #7      HiveImpl.openBox (package:hive/src/hive_impl.dart:111:18)
E/flutter (  752): #8      __DownloadsPageContentState.initState.<anonymous closure> (package:ifi/ui/downloads/downloads_page.dart:40:29)
E/flutter (  752): #9      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
E/flutter (  752): #10     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1060:9)
E/flutter (  752): #11     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:968:5)
E/flutter (  752): #12     _rootRun (dart:async/zone.dart:1190:13)
E/flutter (  752): #13     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (  752): #14     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (  752): #15     _invoke (dart:ui/hooks.dart:267:10)
E/flutter (  752): #16     _drawFrame (dart:ui/hooks.dart:225:3)
E/flutter (  752): 

Yes exactly. The typeId just identifies the class. We could've used the class name but with typeIds you can rename your classes without migration or data loss. Also, they take less space on the disk then a string.

Please read the docs. You need to register your types.

Please read the docs. You need to register your types.

I have registered the types.

final appDocumentDir = await getApplicationDocumentsDirectory();
Hive.init(appDocumentDir.path);
Hive.registerAdapter(DocDownloadAdapter());

Yes exactly. The typeId just identifies the class. We could've used the class name but with typeIds you can rename your classes without migration or data loss. Also, they take less space on the disk then a string.

Got it!

I have registered the types.

@leisim it was working as expected before. But I misunderstood the use of typeId and mistakenly updated the typeId after I changed my schema. I have undone it now, but it's showing me this error.

The database on your device contains a typeId which it does not know how to parse (because you changed it).

Please clear the storage or delete the database file and you will be fine.

Got it working! Thanks for such a speedy reply and solution!

Thanks @leisim and @ashish141199, this point is not clear in this part of the Docs.

Thanks @ashish141199 by the questions.
Thanks @leisim for explaining. : D

Glad I could help. All the best

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ninest picture ninest  路  3Comments

MyoLinOo picture MyoLinOo  路  3Comments

Hopheylalal picture Hopheylalal  路  4Comments

juandiago picture juandiago  路  4Comments

jamesdixon picture jamesdixon  路  3Comments