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

fix: Enum with int type cannot be stored #3983

Open
weeb-destroyer opened this issue Oct 19, 2023 · 1 comment
Open

fix: Enum with int type cannot be stored #3983

weeb-destroyer opened this issue Oct 19, 2023 · 1 comment
Labels
bug Something isn't working dependency This issue has an external dependency needs triage This issue requires triage pkg:hydrated_bloc This issue is related to the hydrated_bloc package

Comments

@weeb-destroyer
Copy link

Description
When I used @JsonValue to specify int value to an enum entry, the value cannot be stored and the retrieved value will always be empty.

Steps To Reproduce

  1. Annotate enum values with @JsonValue of type int
enum Step{
  @JsonValue(1)
  step1,
  @JsonValue(2)
  step2,
  @JsonValue(3)
  step3,
}
  1. Create a param in State with type Map<Step, String>
@freezed
class RegisterState with _$RegisterState {
  factory RegisterState ({
    @Default({}) Map<Step, String> registerSteps,
  }) = _RegisterState;

  factory RegisterState .fromJson(Map<String, dynamic> json) => _$RegisterStateFromJson(json);
}
  1. Modify State
emit(state.copyWith(
        registerSteps: {
          Step.step1: 'done',
          Step.step2: 'doing',
          Step.step3: 'none',
        },
)
  1. Hot Restart to check state

Expected Behavior

The state should be

registerSteps: {
   Step.step1: 'done',
   Step.step2: 'doing',
   Step.step3: 'none',
},        

Actual Behavior

The state showed

registerSteps: {},        

Additional Context

The problem is in the method dynamic _traverseComplexJson(dynamic object),

    } else if (object is Map) {
      _checkCycle(object);
      final map = <String, dynamic>{};
      object.forEach((dynamic key, dynamic value) {
        final castKey = _cast<String>(key);
        if (castKey != null) {
          map[castKey] = _traverseWrite(value).value;
        }
      });
      _removeSeen(object);
      return map;
    }

The map's key is type int so it cannot be casted to String so the value will not be stored

@weeb-destroyer weeb-destroyer added the bug Something isn't working label Oct 19, 2023
@felangel felangel added pkg:hydrated_bloc This issue is related to the hydrated_bloc package needs triage This issue requires triage labels Oct 19, 2023
@SpeedReach
Copy link

I did some investigating and found it's related to this issue.
It happens only on enums, and works fine on other JsonSerialize supported types.

https://github.com/google/json_serializable.dart/blob/85e83641befab6a30526c24950ecfd3fe3f9ce62/json_serializable/README.md?plain=1#L159-L160

@tenhobi tenhobi added the dependency This issue has an external dependency label Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependency This issue has an external dependency needs triage This issue requires triage pkg:hydrated_bloc This issue is related to the hydrated_bloc package
Projects
None yet
Development

No branches or pull requests

4 participants