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

Error generating toJson for Generic freezed class with freezed class as T #855

Closed
socramm9 opened this issue Feb 14, 2023 · 1 comment
Closed
Labels
bug Something isn't working needs triage

Comments

@socramm9
Copy link

I have this Freezed Class that I want to use for DataModel loaded from my Backend

@Freezed(genericArgumentFactories: true)
class DataModel<T> with _$DataModel {
  factory DataModel({
    required String id,
    required bool loading,
    required DateTime lastLoaded,
    required T? data,
  }) = _DataModel;

  factory DataModel.fromJson(
    Map<String, dynamic> json,
    T Function(Object?) fromJsonT,
  ) =>
      _$DataModelFromJson(json, fromJsonT);
}

This generates fine. Next Step Would be to use this class with my Data class and State class. Here a example Using my UserModel.

@freezed
class User with _$User {
  factory CrUser({
    required String id,
    required String fullName,
    required String email,
  }) = _User;

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}

This also generates fine. But if i try to use it together with DataModel:

@freezed
class UsersCubitState with _$UsersCubitState {
  const factory UsersCubitState({
    required Map<String, DataModel<User>> users,
  }) = _UsersCubitState;

  const UsersCubitState._();

  factory UsersCubitState.initial() =>
      const UsersCubitState(users: {});

  factory UsersCubitState.fromJson(Map<String, dynamic> json) =>
      _$UsersCubitStateFromJson(json);
}

I get following error:

[SEVERE] json_serializable on lib/features/users/cubit/users_cubit.dart:

RangeError (index): Invalid value: Only valid value is 0: -1

I don't really know what do do with this error message...

Im I doing something wrong? If necessary I can provide a minimal repository to reproduce this error if needed.

Expected behavior
I would expect to generate my Class without an error.

@socramm9 socramm9 added bug Something isn't working needs triage labels Feb 14, 2023
@socramm9
Copy link
Author

Closing my Issue Found solution here #766

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

1 participant