Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong generator result for empty classes #1081

Open
maeddin opened this issue Sep 26, 2022 · 1 comment · May be fixed by #1082
Open

Wrong generator result for empty classes #1081

maeddin opened this issue Sep 26, 2022 · 1 comment · May be fixed by #1082
Labels
problem An unconfirmed bug.

Comments

@maeddin
Copy link

maeddin commented Sep 26, 2022

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<EmptyClass> {
  @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<EmptyClass> {
  @override
  final int typeId = 58;

  @override
  EmptyClass read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      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
@maeddin maeddin added the problem An unconfirmed bug. label Sep 26, 2022
@maeddin maeddin linked a pull request Sep 26, 2022 that will close this issue
@astubenbord
Copy link

Are there any updates on this? I'm facing the same issue on the latest versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem An unconfirmed bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants