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

Add default value for field on copyWith #1083

Open
markokarajlovic opened this issue May 8, 2024 · 4 comments
Open

Add default value for field on copyWith #1083

markokarajlovic opened this issue May 8, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@markokarajlovic
Copy link

I am using freezed for state class in bloc.

part 'example_state.freezed.dart';

@freezed
class ExampleState with _$ExampleState {
  const ExampleState._();

  const factory ExampleState({
    @Default(ExampleStateBuildStatus.loading) ExampleStateBuildStatus buildStatus,
    ExampleStateListenStatus? listenStatus,
  }) = _ExampleState;
}

enum ExampleStateBuildStatus {
  loading,
  loadSuccess,
}

enum ExampleStateListenStatus {
  error,
}

And every time I emit new state I need to pass null for listenStatus so listener on UI dont trigger every time or I need to override listenWhen to handle it.

emit(state.copyWith(listenStatus: null))

Can you implemented something like this so when I dont pass nothing it will get CopyWithDefault value:

part 'example_state.freezed.dart';

@freezed
class ExampleState with _$ExampleState {
  const ExampleState._();

  const factory ExampleState({
    @Default(ExampleStateBuildStatus.loading) ExampleStateBuildStatus buildStatus,
    @CopyWithDefault(null) ExampleStateListenStatus? listenStatus,
  }) = _ExampleState;
}

enum ExampleStateBuildStatus {
  loading,
  loadSuccess,
}

enum ExampleStateListenStatus {
  error,
}
@TetrixGauss
Copy link

Hello guys! This is an amazing feature to include with the package! Do you know if and when are we going to have it?

@rrousselGit
Copy link
Owner

I don't really have a plan to do that for now.

@shivamkj
Copy link

shivamkj commented Jun 4, 2024

Currently have to manually write copyWithX even with freezed because of lack of this feature. Default value is often needed in CopyWith when working with bloc.

@phyueimon162
Copy link

This is also necessary for my project with Bloc. Please kindly consider adding this feature.

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

5 participants