rrousselGit / freezed

Code generation for immutable classes that has a simple syntax/API without compromising on the features.
https://pub.dev/packages/freezed
1.91k stars 235 forks source link

Add default value for field on copyWith #1083

Open markokarajlovic opened 4 months ago

markokarajlovic commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

shivamkj commented 3 months ago

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 commented 1 week ago

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