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

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
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 ;)
Most helpful comment
I've fixed it by adding
(typeId:0)after@HiveType