Hive: Error running TypeAdapterGenerator

Created on 10 Jan 2020  路  6Comments  路  Source: hivedb/hive

I have an error when calling flutter packages pub run build_runner watch

image

deck.dart :

import 'package:hive/hive.dart';

part 'deck.g.dart';

@HiveType()
class Deck extends TypeAdapter<Deck> {
  @HiveField(0)
  final int id;

  @HiveField(1)
  final String title;

  @HiveField(2)
  final int color;

  Deck(this.id, this.title, this.color);

  @override
  final int typeId = 0;

  @override
  Deck read(BinaryReader reader) {
    int id = reader.readInt();
    String title = reader.readString();
    int color = reader.readInt();

    return Deck(id, title, color);
  }

  @override
  void write(BinaryWriter writer, Deck obj) {
    writer.write(obj.title);
  }
}

main:

void main() {
  runApp(MyApp());
  initHive();
}

void initHive() async {
  final appDocumentsDirectory =
      await path_provider.getApplicationDocumentsDirectory();
  Hive.init(appDocumentsDirectory.path);
  Hive.registerAdapter(DeckAdapter());
  Hive.openBox('selectedDeck');
}

How it can be fixed?

Version

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

Most helpful comment

I've fixed it by adding (typeId:0) after @HiveType

import 'package:hive/hive.dart';

part 'deck.g.dart';

@HiveType(typeId: 0)
class Deck {
  @HiveField(0)
  final int id;

  @HiveField(1)
  final String title;

  @HiveField(2)
  final int color;

  Deck(this.id, this.title, this.color);
}

All 6 comments

I've fixed it by adding (typeId:0) after @HiveType

import 'package:hive/hive.dart';

part 'deck.g.dart';

@HiveType(typeId: 0)
class Deck {
  @HiveField(0)
  final int id;

  @HiveField(1)
  final String title;

  @HiveField(2)
  final int color;

  Deck(this.id, this.title, this.color);
}

Perfect :+1:

This needs to be in the docs, spent hours hunting this down. Unfortunately the line I searched is a screenshot above.

for search engines:

[SEVERE] hive_generator:hive_generator on lib/hive_db/scan_data.dart:
Error running TypeAdapterGenerator
You have to provide a non-null typeId.

Thank you, I spent hours trying to solve this.

Hive is such an amazing package but I wish it had better documentation :(

I wish it had better documentation

That is on my radar and will be part of the next major version ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MyoLinOo picture MyoLinOo  路  3Comments

NourEldinShobier picture NourEldinShobier  路  3Comments

abacaj picture abacaj  路  3Comments

Ferdzzzzzzzz picture Ferdzzzzzzzz  路  3Comments

jamesdixon picture jamesdixon  路  3Comments