Hive: Problems when changing model and its adapter

Created on 15 Jan 2020  路  7Comments  路  Source: hivedb/hive

I have a problem with type adapter
That's how I changed the Deck, just removed int id :

image

But when I register the adapter as an output I see this:

E/flutter (12595): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast
E/flutter (12595): #0      DeckAdapter.read (package:my_flashcards/models/deck.g.dart:20:17)
E/flutter (12595): #1      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:313:31)
E/flutter (12595): #2      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:258:26)
E/flutter (12595): #3      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:16:26)
E/flutter (12595): #4      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:37:12)
E/flutter (12595): <asynchronous suspension>
E/flutter (12595): #5      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:74:29)
E/flutter (12595): <asynchronous suspension>
E/flutter (12595): #6      BoxBaseImpl.initialize (package:hive/src/box/box_base_impl.dart:82:20)
E/flutter (12595): #7      HiveImpl._openBox (package:hive/src/hive_impl.dart:97:17)
E/flutter (12595): <asynchronous suspension>
E/flutter (12595): #8      HiveImpl.openBox (package:hive/src/hive_impl.dart:114:18)
E/flutter (12595): #9      initHive (package:my_flashcards/main.dart:19:14)
E/flutter (12595): <asynchronous suspension>
E/flutter (12595): #10     main (package:my_flashcards/main.dart:10:9)
E/flutter (12595): #11     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter (12595): #12     _rootRun (dart:async/zone.dart:1126:13)
E/flutter (12595): #13     _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (12595): #14     _runZoned (dart:async/zone.dart:1518:10)
E/flutter (12595): #15     runZoned (dart:async/zone.dart:1502:12)
E/flutter (12595): #16     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (12595): #17     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (12595): #18     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

That's how Hive is initialized:
image

Maybe the problem is that by changing Deck model, 0 field is now of type String instead of int as it was earlier.

Version

  • Platform: iOS, Android
  • Flutter version: 1.12.13
  • Hive version: 1.3.0

    • hive_flutter: 0.3.0+1

bug

Most helpful comment

If you change an existing field, it is important to follow the rules.

Then it should work.

All 7 comments

@sergiyvergun Regenerating your adapter would work as per my understanding.
Delete your current adapter (DeckAdapter) and run flutter packages pub run build_runner build again.
Let me know if this worked.
If did'nt, I will look more into it 馃憤

@annshsingh It doesn`t work. The same output

@sergiyvergun Interesting.
Just to clarify that your updated DeckAdapter should look something like this -

class DeckAdapter extends TypeAdapter<Deck> {
  @override
  final typeId = 0;

  @override
  Node read(BinaryReader reader) {
    var numOfFields = reader.readByte();
    var fields = <int, dynamic>{
      for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Deck(
      fields[0] as String,
      fields[1] as int,
    );
  }
.
.
.

Here the 0th field is String, and 1st field is an int.

Also are you using
hive_generator: ^0.7.0
build_runner: ^1.7.3
To generate TypeAdapters for Hive ?

@annshsingh yes, it looks the same.

image

image

@sergiyvergun
Looks alright to me.
Let me see if I can replicate the issue at my end.

Just try one last thing, after regenerating your adapter, run flutter clean. Not sure if this'll work, but worth a try.

If you change an existing field, it is important to follow the rules.

Then it should work.

@leisim It works now. The problem was with that

Don't change the field numbers for any existing fields.

Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hopheylalal picture Hopheylalal  路  4Comments

maxim-saplin picture maxim-saplin  路  3Comments

Ferdzzzzzzzz picture Ferdzzzzzzzz  路  3Comments

rupamking1 picture rupamking1  路  3Comments

abacaj picture abacaj  路  3Comments