Skip to content

BUG: Hive generator generates nullable HiveLists #644

@vandir

Description

@vandir

Minimal working example:

import 'package:hive/hive.dart';

part 'person.g.dart';

@HiveType(typeId: 1)
class Person extends HiveObject {
  @HiveField(0)
  HiveList<Person> friends;

  Person(this.friends);
}

The (redacted) generated code is as follow:

.
.
.
class PersonAdapter extends TypeAdapter<Person> {
  @override
  final int typeId = 1;

  @override
  Person read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Person(
      (fields[0] as HiveList)?.castHiveList(),
    );
  }
.
.
.

The (type) error is:

The argument type 'HiveList<Person>?' can't be assigned to the parameter type 'HiveList<Person>'

The line (fields[0] as HiveList)?.castHiveList() is wrong because is nullable but friends is not. Here the problem is the use of the ?. operator over a non nullable expression ((fields[0] as HiveList))

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededproblemAn unconfirmed bug.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions