isar / isar/hive

Wrong generator result for empty classes

Open
#1,081 1 comment 0 reactions 0 assignees View on GitHub
problem
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Steps to Reproduce**
Create an empty class with the HiveType annotation.

```
@HiveType(typeId: 0)
class EmptyClass {}
```

Start the generator. The result is:

```
class EmptyClassAdapter extends TypeAdapter {
@override
final int typeId = 58;

@override
EmptyClass read(BinaryReader reader) {
return EmptyClass();
}

@override
void write(BinaryWriter writer, EmptyClass obj) {
writer.writeByte(0);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is EmptyClassAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```

But the correct result should be:
```
class EmptyClassAdapter extends TypeAdapter {
@override
final int typeId = 58;

@override
EmptyClass read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return EmptyClass();
}

@override
void write(BinaryWriter writer, EmptyClass obj) {
writer.writeByte(0);
}

@override
int get hashCode => typeId.hashCode;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is EmptyClassAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
```

**Version**
- Platform: -
- Flutter version: 3.3.2
- Hive version: 2.2.3
- Hive generator version: 1.1.3

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the Hive generator's handling of an empty class annotated with @HiveType, using the issue's reproduction and generated adapter output. Compare the current read method with the expected output: completion means an empty class still generates the field-count and fields-reading logic while preserving the shown write method.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
build-system, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.