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

Setting the flag @Freezed(toJson: false) creates an error in the generated freezed files when we define a custom toJson. #1070

Open
PackRuble opened this issue Apr 13, 2024 · 0 comments
Assignees
Labels
bug Something isn't working needs triage

Comments

@PackRuble
Copy link

Describe the bug
Let's assume that we need our own fromJson and toJson definitions for the Model. We set the appropriate flags in the @Freezed(fromJson: false, toJson: false) annotation. However, the generated files produce an error:

// analyzer: Missing concrete implementation of 'Model.toJson'.

class _$ModelImpl implements _Model {
  const _$ModelImpl();

  @override
  String toString() {
    return 'Model()';
  }

  @override
  bool operator ==(Object other) {
    return identical(this, other) ||
        (other.runtimeType == runtimeType && other is _$ModelImpl);
  }

  @override
  int get hashCode => runtimeType.hashCode;
}

To Reproduce

import 'package:freezed_annotation/freezed_annotation.dart';

part 'model.freezed.dart';

@Freezed(fromJson: false, toJson: false)
class Model with _$Model {
  const factory Model() = _Model;

  factory Model.fromJson(Map<String, dynamic> json) => Model();

  Map<String, dynamic> toJson() => {};
}

Expected behavior
Adding a constant private constructor solves this problem, because our toJson method becomes an ordinary method of the Model class directly. So it turns out that we have to proceed according to the documentation Adding getters and methods to our models. Is this probably expected behavior?

@PackRuble PackRuble added bug Something isn't working needs triage labels Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

2 participants