Skip to content

Add default value for field on copyWith #1083

Open
@markokarajlovic

Description

@markokarajlovic

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,
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestquestionFurther information is requested

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions