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

BUG: Hive generator generates nullable HiveLists #644

Closed
vandir opened this issue Apr 24, 2021 · 7 comments · Fixed by #728
Closed

BUG: Hive generator generates nullable HiveLists #644

vandir opened this issue Apr 24, 2021 · 7 comments · Fixed by #728
Labels
help wanted Extra attention is needed problem An unconfirmed bug.

Comments

@vandir
Copy link

vandir commented Apr 24, 2021

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))

@vandir vandir added the problem An unconfirmed bug. label Apr 24, 2021
@vandir vandir changed the title Hive generator generates nullable HiveLists BUG: Hive generator generates nullable HiveLists Apr 29, 2021
@vandir
Copy link
Author

vandir commented May 26, 2021

up

@vandir
Copy link
Author

vandir commented Jun 14, 2021

@themisir

@emme1444
Copy link

Would be nice to get this solved. Any updates?

As a work-around I modify the generated code from this:

(fields[0] as HiveList)?.castHiveList(),

to this:

(fields[0] as HiveList).castHiveList(),

Just ensuring this is an accepted way to work around the problem? Although, ideally I wouldn't want to modify the generated code. Obviously.

@thirdbody
Copy link

Seconded. Still experiencing this issue. I don't see why replacing ?. with . wouldn't be a suitable workaround in the meantime.

@thirdbody
Copy link

After working with Hive for a few days I've had no trouble with replacing ?. with . @emme1444 . Definitely a suitable workaround.

@themisir themisir added the help wanted Extra attention is needed label Jul 14, 2021
@themisir
Copy link
Contributor

I'm a bit busy those days, so fixing this issue might take some time. If anybody wants to work on this issue PRs are appreciated (let me know if you need assistance) 😊

@thirdbody
Copy link

Got it, found the issue. I'll open a PR soon.

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

Successfully merging a pull request may close this issue.

5 participants