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

Arguments are missing in constructor of TypeAdapter from extended class #236

Closed
giandifra opened this issue Feb 22, 2020 · 8 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@giandifra
Copy link

Steps to Reproduce
I have created TypeAdapter from this comment to use extended class.

Code sample
pet.dart

import 'package:hive/hive.dart';

class Pet extends Equatable {
  @HiveField(0)
  final String name;

  @HiveField(1)
  final int age;

  Pet(this.name, this.age);

  @override
  List<Object> get props => [name, age];
}

dog.dart

import 'package:hive/hive.dart';
import 'pet.dart';

part 'dog.g.dart';

@HiveType(typeId: 1)
class Dog extends Pet {
  Dog(String name, int age) : super(name, age);
}

dog.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'dog.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class DogAdapter extends TypeAdapter<Dog> {
  @override
  final typeId = 1;

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

  @override
  void write(BinaryWriter writer, Dog obj) {
    writer
      ..writeByte(2)
      ..writeByte(0)
      ..write(obj.name)
      ..writeByte(1)
      ..write(obj.age);
  }
}

How you can see, there is an error in the constructor of Dog in TypeAdapter, arguments are missing.

Version

  • Flutter version: 1.15.2-pre.36
  • Flutter channel: master
  • hive: ^1.4.0+1
  • hive_flutter: ^0.3.0+1
  • hive_generator: ^0.7.0
@giandifra giandifra added the problem An unconfirmed bug. label Feb 22, 2020
@giandifra giandifra changed the title Miss arguments in constructor of TypeAdapter from extended class Arguments are missing in constructor of TypeAdapter from extended class Feb 22, 2020
@simc
Copy link
Member

simc commented Feb 22, 2020

Unfortunately, only zero-argument constructors and constructors which use Initializing parameters (like "Pet(this.name, this.age)") can be used.

@simc simc added enhancement New feature or request and removed problem An unconfirmed bug. labels Feb 22, 2020
@yringler
Copy link

yringler commented Jul 23, 2020

I just stumbled into this.
For others who see this: that just means that a base class has to be non-final, and either have no constructor or a constructor with no arguments.
I do like my finals, but not too bad.

@mohammadne
Copy link

my problem is that I am using JsonSerializable for my classes and if I not specify the constructor, I will get
The class `Pet` has no default constructor.
so I have to use constructor for that.

any help is appreciated.

@mohammadne
Copy link

is there any way to fix the constructor issue ?

@yringler
Copy link

yringler commented Nov 1, 2020

If you don't specify a constructor, then there's an implicit default constructor. Can you paste your class?

@mohammadne
Copy link

If you don't specify a constructor, then there's an implicit default constructor. Can you paste your class?

#442 (comment)

@themisir
Copy link
Contributor

themisir commented Nov 4, 2020

Duplicate of #442

@qwertie
Copy link

qwertie commented Jul 4, 2024

@simc I'm struggling to figure out how to migrate old data into a new object format. You say only this. parameters can be used, but the documentation says that "Fields can be...changed from public to private or vice versa". But the compiler rejects this._foo as a constructor parameter, so it would seem that making fields private is impossible. I guess I have to use fields with names like private_foo? And apparently I will also need to have fields that aren't used - fields whose only purpose is to be migrated in the constructor, and are never used after that. They should be normal constructor parameters, not fields, but this isn't allowed? This is poor UX (for developers) and wastes memory.

Moreover, it looks like certain fields that should be final must be mutable so that the constructor can change them, since a parameter x cannot use both this.x syntax and : x = expr syntax at the same time. And presumably it is also not possible to use a factory constructor like MyClass.fromHive() for the migration code, since factory constructors don't use "this." parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants