0

Good day everyone! Let's imagine I am using Flutter Cubit with @freezed for an authentication screen. There are several ways on how to structure the state but all come with a problem I can't find a way around. I normally design my States like this:

@freezed
class SignUpState with _$SignUpState {
  const factory SignUpState({
    @Default('') String email,
    @Default('') String password,
    InputPasswordError? passwordError,
    InputEmailError? emailError,
    AppError? error,
    @Default(false) bool loadingStatus
    @Default(false) bool passwordVisibilityStatus,
    @Default(false) bool successBottomSheetVisibleStatus,
  }) = _SignUpState;
}

If I now emit a new State when authentication is done with successBottomSheetVisibleStatus set to true it will show a bottom sheet form blocListener. On BottomSheet().show().then(cubit.onSuccessBottomSheetClosed) the status of successBottomSheetVisibleStatus will be set back to false. The Problem here is if I may want to emit a different State while the bottom sheet is still visible it will get re triggered. I know i can just check in listenWhen: (p, c) => p.status != c.status but this will prevent other events firing if i have multiple functionalities inside the listener.

I could rewrite the state into a different pattern like the following:

@freezed
class SignUpState with _$SignUpState {
  const factory SignUpState.initial(...) = SignUpStateInitial;
  const factory SignUpState.navigateToHome() = SignUpStateNavigateToHome;
}

This approach would fix these issue BUT i always need the same state since. I also could fix this by adding buildWhen: (p, c) => c is SignUpStateInitial but that seems a bit to hacky and will cause other problems, since this check does not happen on the first state.

How to design the State in a way for it to be able to handle all these requirements?

apatrck01
  • 89
  • 1
  • 6
  • 1
    I often work with enums inside my single state. I have all my information in every state and also a status in my enum variable. You can check this out https://bloclibrary.dev/#/flutterlogintutorial (it's with Bloc, but if you ignore the event part it should be the same) – Ozan Taskiran Jan 10 '23 at 11:57

0 Answers0