-
Notifications
You must be signed in to change notification settings - Fork 408
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
[hive_generator] not generating type adapter for freezed class #795
Comments
I don't use the generator personally, but as I understand, the It looks like for your freezed classes, you may have to roll your adapters manually. |
Thanks for the reply, occording to #225 the HiveType should be positioned above the factory and not the class (since freezed will generate the propper class and copy the annotation) and the above or before positioning of dart annotations shouldn't matter. Generated class @JsonSerializable()
@ColorConverter()
@HiveType(typeId: 0, adapterName: 'WledDeviceAdapter')
class _$_WledDevice implements _WledDevice {
const _$_WledDevice({
@HiveField(0) required this.address,
@HiveField(1) required this.name,
@HiveField(2) this.status = DeviceStatus.functional,
@HiveField(3) this.nameIsCustom = false,
@HiveField(4) this.isSaved = false,
@HiveField(5) this.isEnabled = true,
@HiveField(6) this.brightness = 0.9,
@HiveField(7) @JsonKey() this.color = ColorConverter.defaultColor
});
@override // device IP (can also be hostname if applicable)
@HiveField(0)
final String address;
@override // device display name ("Server Description")
@HiveField(1)
final String name;
@JsonKey(defaultValue: DeviceStatus.functional)
@override // Current connection status
@HiveField(2)
final DeviceStatus status;
@JsonKey(defaultValue: false)
@override // If the light name is custom, the name returned
@HiveField(3)
final bool nameIsCustom;
@JsonKey(defaultValue: false)
@override // if the wled device is saved to the local device
@HiveField(4)
final bool isSaved;
@JsonKey(defaultValue: true)
@override // Disabled devices don't get polled or show up in the list
@HiveField(5)
final bool isEnabled;
@JsonKey(defaultValue: 0.9)
@override // There are two vars for brightness to discern
@HiveField(6)
final double brightness;
@override // the currently active device color
@HiveField(7)
@JsonKey()
final Color color;
|
@themisir something related to this build_runner output perhaps? [WARNING] No actions completed for 17.6s, waiting on:
- hive_generator:hive_generator on lib/core/app/app_router.dart
- hive_generator:hive_generator on lib/features/device_add/bloc/device_add_bloc.dart
- hive_generator:hive_generator on lib/features/device_control/bloc/device_control_bloc.dart
- hive_generator:hive_generator on lib/features/device_list/bloc/device_list_bloc.dart
- hive_generator:hive_generator on lib/features/device_add/repository/device_add_repository.dart
.. and 5 more |
I have this same issue, it happened when I upgraded to the latest freezed version |
hmm odd, @rrousselGit I can't see anything in the changelog that would directly break this? |
Freezed doesn't care about converters. It's json_serializable that deals with them. But maybe changing the Frezed version changes the json_serializable version you're using |
I managed to workaround this issue by adding the following to force generation from the freezed file @freezed
class _Dummy with _$_Dummy {
const factory _Dummy() = __Dummy;
factory _Dummy.fromJson(Map<String, dynamic> json) => _$_DummyFromJson(json);
} Still, we need to figure out what is the issue. |
I tried both
and
neither worked for me. I've been experimenting with package versions, overriding freezed and hives dependencies, no luck so far. |
@casvanluijtelaar did you have the json_serializable package in dev dependencies? |
Yes |
Same issue for me. Tried everything. Downgrade to the older versions helps only.
|
Same issue happening, fixed only downgrading all the dependencies as @yura2000 said in the comment above mine. |
Yes hive generator 1.1.1 is incompatible with json_serializable 6. |
Do you know why it is incompatible? Then we can create a pull request |
no idea, but now I am stuck cause the last version of flutter is incompatible with auto_route 2.1.0 generator, but I cannot upgrade to newest version of the generator cause it requires json_serializable 6 .... |
The issue in the |
EDIT: Please ignore everything below, a new day revelas that what was working yesterday is not working today. I have no idea why this is. I'll leave the existing comment in case it gives an idea to someone else. Original post below. OK, not sure how much this will help the conversation as I'm pretty new to Dart/Flutter and I haven't spent any time looking into how code generation works but I ran into this problem and was trying a few things to see if I could get it to work. After a lot of digging around I've discovered that there are a couple of things impacting adapter generation in my project. Like @yura2000, I'm using targets:
$default:
builders:
hive_generator:
options:
runs_before:
- auto_route_generator After doing this I expected everything to 'just' work. It didn't. What I have also discovered is that there needs to be a newline at the end of the source file for the adapter to be correctly generated. E.g. import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hive/hive.dart';
part 'demo.freezed.dart';
part 'demo.g.dart';
@freezed
class Demo with _$Demo {
@HiveType(typeId: 0, adapterName: 'DemoAdapter')
factory Demo({@HiveField(0) required int val,}) = _Demo;
factory Demo.fromJson(Map<String, dynamic> json) => _$DemoFromJson(json);
} Having just the above content in the source file meant the adapter was NOT generated. Modifying the source file so there is a newline after the closing brace ( import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hive/hive.dart';
part 'demo.freezed.dart';
part 'demo.g.dart';
@freezed
class Demo with _$Demo {
@HiveType(typeId: 0, adapterName: 'DemoAdapter')
factory Demo({@HiveField(0) required int val,}) = _Demo;
factory Demo.fromJson(Map<String, dynamic> json) => _$DemoFromJson(json);
}
I've put together a small project that demonstrates this, feel free to check it out and run build_runner and look at the generated rresults. With the newline at the end of the Hopefully this can help people understand why one thing or another doesnt appear to be working for them. P.S. One annoying thing is, without having |
I also removed auto router and it fixed it for me as well, this is with the latest versions of freezed, json_serializable, and hive |
Any new workarounds? |
@yura2000 Hey, could you share how you managed to use the forked hive package? Because the repo has 3 packages inside |
@AntonLT you can use my fork https://github.com/yura2000/hive |
@yura2000 Yeah, thank you. But how have you added the packages in dependencies in pubspec.yaml? |
In the past,when I've run into this issue, I was able to temporarily resolve it by making some sort of modification to the file of the Freezed class that was not properly generating the TypeAdapter. Following the non-confirmed solution @andye2004 posted above, instead of modifying the actual class, I was able to resolve the issue and generate the TypeAdapter by simply adding a commented line underneath the Freezed class. If the error appears again, I plan to simply delete that comment to trigger a rebuild and expect that to work. In short: triggering a genuine modification of a Freezed file seems to resolve the issue, at least temporarily. |
Thanks @yura2000 your fork did wonders. Here's how i used it Add to pubspec.yaml environment:
sdk: ">=2.17.0-266.5.beta <3.0.0"
dependencies:
...
# For regular Unions
freezed_annotation: ^2.0.1
# Lightweight and blazing fast key-value database written in pure Dart. Strongly encrypted using AES-256.
hive: ^2.1.0
hive_flutter:
git:
url: https://github.com/yura2000/hive.git
ref: master
path: hive_flutter/
...
dev_dependencies:
...
freezed: ^2.0.2
json_serializable: ^6.1.6
hive_generator:
git:
url: https://github.com/yura2000/hive.git
ref: master
path: hive_generator/ And in my part 'user_dto.freezed.dart';
part 'user_dto.g.dart';
part 'user_dto.hive.dart';
@immutable
@Freezed(makeCollectionsUnmodifiable: false)
class UserDTO with _$UserDTO {
@HiveType(typeId: 1, adapterName: 'UserDTOAdapter')
const factory UserDTO({
@HiveField(1) String? uid,
@HiveField(2) String? firstName,
}) = _UserDTO;
const UserDTO._();
factory UserDTO.fromJson(Map<String, dynamic> json) => _$UserDTOFromJson(json);
} And if you want to extend HiveObject "to manage your objects easily..." part 'user_dto.freezed.dart';
part 'user_dto.g.dart';
part 'user_dto.hive.dart';
@unfreezed
class UserDTO extends HiveObject with _$UserDTO {
@HiveType(typeId: 1, adapterName: 'UserDTOAdapter')
factory UserDTO({
@HiveField(1) String? uid,
@HiveField(2) String? firstName,
}) = _UserDTO;
UserDTO._();
factory UserDTO.fromJson(Map<String, dynamic> json) => _$UserDTOFromJson(json);
} PS: You must be on the latest version of freezed_annotation to use the |
So i spent some times trying to figure out this issue and here's what I've found.
so I think the issue was hive generator was not passing the fields to inherited classes, because even thought in the lastest version hive supports inhertance I was still getting the same issue. so my solution was to not use freezed so the type adapter is created directly from the class. and it has worked for me. here's my pubspec.yaml
and the hive model class looks like this
|
try this command |
Hi guys, i am using hive, freeze and autoroute. Unfortunately, the hive adapter is not generated. |
its simple
…On Wed, Nov 2, 2022 at 11:56 PM michelinaFolino ***@***.***> wrote:
Hi guys,
i am using hive, freeze and autoroute. Unfortunately, the hive adapter is
not generated.
I used @yura2000 <https://github.com/yura2000> 's solution and it works.
Do you know when the official version will be released?
—
Reply to this email directly, view it on GitHub
<#795 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AS3BVHOPRSZA53G5C3YRWELWGKW6NANCNFSM5EVXQ4MA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Hi guys, news? |
Please guys if you can check this problem, Cause I need both freezed and hive adaptor, where is my problem if I put
after class, the hive generator won't work for me, and no adaptor is generated while using it before the class name, it is generated but I did check the .g file and it only returns an empty object without initializing any field from the list of fields This is one code I used, but I did try without abstract, without HiveObject.... and many other suggested solutions online... but still won't work. This is considered a big problem for me, cause now I have to sacrifice Freezed with its amazing feature, especially
|
as far as I know, this library is going to be deprecated very soon in favor of isar. |
Hello all! If you're using Hive and Freezed, for adapter generation, do next.
It is important to observe the indents, like this: Flutter 3.7.5/Dart 2.19.2 hive_generator ^2.0.0 |
@poltavskiymc the |
@alejandro-rios you don't need to have anything else on the global_options:
hive_generator:
runs_before:
- freezed Edit: I've changed the main constructor to private and it worked. |
I'm attempting to generate a type adapter for my freezed class basing myself of the examples in #225. It will generate the
DeviceStatus
adapter just fine. but not theWledDevice
no code gets generated and no errors are thrownpubspec.yaml & build_runner output
The text was updated successfully, but these errors were encountered: