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

InvalidType with Freezed Union/Sealed classes with json serialization in other Freezed classes. #920

Closed
JoanSchi opened this issue May 22, 2023 · 3 comments
Assignees
Labels
bug Something isn't working needs triage

Comments

@JoanSchi
Copy link

Hi,

I am using Freezed Union/Sealed classes with json serialization in other Freezed classes. In de past this worked fine, but now I get a InvalidType error.

Example:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'model.freezed.dart';
part 'model.g.dart';

@freezed
sealed class Model with _$Model {
  factory Model.first(String a) = First;
  factory Model.second(int b, bool c) = Second;

  factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
}
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:simple_test/src/model.dart';

part 'test.freezed.dart';
part 'test.g.dart';

@freezed
class Test with _$Test {
  const factory Test({
    required First first,
  }) = _Test;

  factory Test.fromJson(Map<String, Object?> json) => _$TestFromJson(json);
}

Error
Could not generate fromJson code for first.
To support the type InvalidType you can:

Expected behavior
Generated the code

Doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.10.1, on openSUSE Tumbleweed 6.3.1-1-default, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2021.3)
[✓] VS Code (version 1.68.1)
[✓] Connected device (3 available)
[✓] Network resources

pubspec

name: simple_test
description: A starting point for Dart libraries or applications.
version: 1.0.0

repository: https://github.com/my_org/my_repo

environment:
sdk: ^3.0.1

Add regular dependencies here.

dependencies:
freezed_annotation: ^2.2.0
json_annotation: ^4.8.1

path: ^1.8.0

dev_dependencies:
build_runner: ^2.4.4
freezed: ^2.3.4
json_serializable: ^6.7.0
lints: ^2.0.0
test: ^1.21.0

@JoanSchi JoanSchi added bug Something isn't working needs triage labels May 22, 2023
@SunlightBro
Copy link

Known issue with analyzer 5.13.0
In your pubspec.yaml add

dependency_overrides:
  analyzer: 5.12.0

@rrousselGit
Copy link
Owner

Closing in favour of #916

@oowliq
Copy link

oowliq commented Jun 8, 2023

Known issue with analyzer 5.13.0 In your pubspec.yaml add

dependency_overrides:
  analyzer: 5.12.0

I ❤️‍🔥 U

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

4 participants