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

registerAdapter does not work as shown in the example #1293

Open
michelemazzei opened this issue May 31, 2024 · 3 comments
Open

registerAdapter does not work as shown in the example #1293

michelemazzei opened this issue May 31, 2024 · 3 comments

Comments

@michelemazzei
Copy link

michelemazzei commented May 31, 2024

To register a serializable object in Hive the method to use is:

Hive.registerAdapter('Bee', Bee.fromJson);

But, if you use the example code:

class Person {
  String name;
  int age;
  Person(this.age, this.name); //<-- I had to add this, because name and age are not nullable

  factory Person.fromJson(Map<String, dynamic> json) {
    return Person(0, '')
      ..name = json['name'] as String
      ..age = json['age'] as int;
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'age': age,
    };
  }
}
 Hive.registerAdapter('Person', Person.fromJson);

It does not compile the register Adapter method.
More over if I try to force to compile (with a cast) it takes an error at runtime.
Cannot use 4.0.0.dev2 , must use the 2.2.3 with @HiveType and HiveObject.

@oscarshaitan
Copy link

the registration is incorrect should be something like

  Hive.registerAdapter(Person Adapter());

@Krzysztof-Lempicki
Copy link

@oscarshaitan could you explain?

@oscarshaitan
Copy link

oscarshaitan commented Aug 2, 2024

All this is with hive 2.2.3
You have this enum for example
Screenshot 2024-08-02 at 10 05 34
with all the hive annotation
hive will create an adapter for you
inside 'distance_unit.g.dart'

Screenshot 2024-08-02 at 10 06 47
Thats the adapter you have to register
Screenshot 2024-08-02 at 10 07 09

if you dont use the hive annotations you still can do it but you have to create the adapter by hand

right now if you want to play with the V4 I'll suggest you to try other solution like sembast that is json oriented
the main project has been 2 years radio silent and the v4 close to 1 year. Im using currently Hive 2.2.3 but in few weeks Ill do the change.
I did a bench quite tailored to my needs
https://www.linkedin.com/pulse/replacing-hive-seeking-more-robust-flexible-storage-oscar-tigreros-hd5he/?trackingId=UZnb5cTgnQ%2BCx%2Bq8rI%2BiHw%3D%3D

but could help you understand what you can do

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

No branches or pull requests

3 participants