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

Generator not building file for Freezed class #987

Open
aardrop opened this issue Jun 1, 2022 · 4 comments
Open

Generator not building file for Freezed class #987

aardrop opened this issue Jun 1, 2022 · 4 comments
Labels
problem An unconfirmed bug.

Comments

@aardrop
Copy link

aardrop commented Jun 1, 2022

Steps to Reproduce
We use Hive for our settings and we have it attached to a Freezed class as well. We are on the most recent version of both platforms. The freezer generator file builds without fail but the hive file does not. Actually, it only will build if I add or remove the 'abstract' keyword. Once I change the 'abstract' (either adding or removing it) it will build the generator file BUT the next time the generator runs it will remove the file again and I will have to change it again to trigger a rebuild.
``

Code sample

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

import 'package:../domain/settings/settings.dart';

part 'settings_dto.freezed.dart';
part 'settings_dto.g.dart';

@freezed
class SettingsDto with _$SettingsDto {
  @HiveType(typeId: 1, adapterName: 'SettingsDtoAdapter')
  const SettingsDto._();
  @HiveType(typeId: 0)
  const factory SettingsDto({
    @HiveField(0) final String? id,
    @HiveField(1) final String? units,
  }) = _SettingsDto;

  factory SettingsDto.fromDomain(Settings settings) {
    return SettingsDto(
      id: settings.id,
      units: settings.units.name,
    );
  }

  Units stringToEnumUnits(String unitsStr) {
    Units u =
        Units.values.firstWhere((e) => e.toString() == 'Units.' + unitsStr);
    return u;
  }

  Settings toDomain() => Settings(
        id: id,
        units: units == null ? Units.imperial : stringToEnumUnits(units!),
      );
}

Version

  • Flutter version: version 2.7<3.0 (haven't tested it on 3 yet)
  • Hive version: hive: ^2.2.1
    hive_flutter: ^1.1.0
    hive_generator: ^1.1.3
@aardrop aardrop added the problem An unconfirmed bug. label Jun 1, 2022
@Nightwelf
Copy link

same problem :(

@nullxception
Copy link

nullxception commented Aug 24, 2022

Hi. I had the same issues recently, and managed to solve it by reordering the builder sequence so hive_generator will run after freezed but before json_serializable.

# build.yaml
global_options:
  freezed:freezed:
    runs_before:
      - hive_generator:hive_generator
  hive_generator:hive_generator:
    runs_before:
      - json_serializable:json_serializable

though I'm not sure if it'll bring another issues to the table or not.

@Rexios80
Copy link

This should be in the readme

@alguintu
Copy link

alguintu commented Sep 30, 2023

Using freezed 2.4.3 with Hive now generates ObjectImplAdapter instead of ObjectAdapter in the .g.dart file and breaks existing imports.

Breaking change in Freezed 2.4.3

This is supposed to be intended behavior, and hive is not supposed to be using the internal Impl class as per Remi.
Importing the new ObjectImplAdapter still works though, for now.

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

No branches or pull requests

5 participants