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

Support extending Freezed #1028

Open
lirantzairi opened this issue Dec 26, 2023 · 1 comment
Open

Support extending Freezed #1028

lirantzairi opened this issue Dec 26, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@lirantzairi
Copy link

I'd like to use my own default Freezed annotation that has default parameter values (e.g toJson: true).
So Instead of:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'my_model.freezed.dart';

@Freezed(toJson: true)
class MyModel with _$MyModel {
  const factory MyModel({
    required String name,
  }) = _MyModel;
}

I want:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'my_model.freezed.dart';

class MyFreezed extends Freezed {
  const MyFreezed({
    super.unionKey,
    super.unionValueCase,
    super.fallbackUnion,
    super.copyWith,
    super.equal,
    super.toStringOverride,
    super.fromJson,
    super.toJson = true,
    super.map,
    super.when,
    super.makeCollectionsUnmodifiable,
    super.addImplicitFinal,
    super.genericArgumentFactories,
  });
}

@MyFreezed()
class MyModel with _$MyModel {
  const factory MyModel({
    required String name,
  }) = _MyModel;
}

Note that I don't want to create an instance of Freezed, instead I want to subclass Freezed so I can also set other parameters as needed.

The problem is that this crashes the build runner:

[SEVERE] freezed on lib/my_model.dart:

Null check operator used on a null value

Package versions:

  • freezed_annotation: 2.4.1
  • build_runner: 2.4.7
  • freezed: 2.4.6
@rrousselGit
Copy link
Owner

Extending the annotation is not supported.

You can do:

const myFreezed = Freezed(toJson: true)

@rrousselGit rrousselGit changed the title Cannot extend Freezed annotation Support extending Freezed Dec 26, 2023
@rrousselGit rrousselGit added enhancement New feature or request and removed bug Something isn't working needs triage labels Dec 26, 2023
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

2 participants