Home > Back-end >  How to fix absence of state when adding another emit in flutter/bloc?
How to fix absence of state when adding another emit in flutter/bloc?

Time:11-27

I am new at flutter and trying to find out how bloc works... I have a state inside cubits and when I try to emit WaitingAuth(this is CircularProgressIndicator when I send signUp request) then my state disappears...

This emit(WaitingAuth()) I insert when I send request, the server returns 400 error and request body looks like this:

email: "", password: "", and so on..

But if I remove this emit(WaitingAuth()) then everything works fine and state looks like this:

email: "[email protected]", password: "dfdsfdffsd", and so on..

I don't understand why my state disappears...

This is my cubit file:

class AuthCubit extends Cubit<AuthState> {
  AuthCubit() : super(AuthState(
    email: "",
    password: "",
    firstName: "",
    lastName: "",
    genderUuid: "",
    ageGroupUuid: "",
    countryUuid: "",
  ));

  final ApiService _apiService = ApiService();

  void setCountryUuid(String countryUuid) => emit(state.copyWith(countryUuid: countryUuid));

  void setFirstName(String firstName) => emit(state.copyWith(firstName: firstName));

  void setLastName(String lastName) => emit(state.copyWith(lastName: lastName));

  void setEmail(String email) => emit(state.copyWith(email: email));

  void setGenderUuid(String genderUuid) => emit(state.copyWith(genderUuid: genderUuid));

  void setAgeGroupUuid(String ageGroupUuid) => emit(state.copyWith(ageGroupUuid: ageGroupUuid));

  void setPassword(String password) => emit(state.copyWith(password: password));

  Future signUp() async {
    emit(const WaitingAuth()); // If I remove this emit then everything works and Ssign up request works well
    try {
      final data =  await _apiService.signUp(
        email: state.email ?? "",
        password: state.password ?? "",
        firstName: state.firstName ?? "",
        lastName: state.lastName ?? "",
        genderUuid: state.genderUuid ?? "",
        ageGroupUuid: state.ageGroupUuid ?? "",
        countryUuid: state.countryUuid ?? ""
      );
      if (data['success']) {
        log(data.toString());
        emit(const SuccessAutentification());
      }
    } on DioError catch (ex) {
      log("cubit === ${ex.response!.data.toString()}");
    } catch (e) {
      log(e.toString());
      rethrow;
    }
  }
}

And this is my state file:

class AuthState extends Equatable {
  final String? email;
  final String? password;
  final String? firstName;
  final String? lastName;
  final String? genderUuid;
  final String? ageGroupUuid;
  final String? countryUuid;

  const AuthState({
    this.email,
    this.password,
    this.firstName,
    this.lastName,
    this.genderUuid,
    this.ageGroupUuid,
    this.countryUuid,
  });

  AuthState copyWith({
    String? email,
    String? password,
    String? firstName,
    String? lastName,
    String? genderUuid,
    String? ageGroupUuid,
    String? countryUuid,
  }) {
    return AuthState(
      email: email ?? this.email,
      password: password ?? this.password,
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      genderUuid: genderUuid ?? this.genderUuid,
      ageGroupUuid: ageGroupUuid ?? this.ageGroupUuid,
      countryUuid: countryUuid ?? this.countryUuid,
    );
  }

  @override
  List<Object?> get props =>
      [email, password, firstName, lastName, genderUuid, ageGroupUuid, countryUuid];
}

class SuccessAutentification extends AuthState {
  const SuccessAutentification() : super();
}

class ErrorAuthentification extends AuthState {
  const ErrorAuthentification() : super();
}

class WaitingAuth extends AuthState {
  const WaitingAuth() : super();
}

CodePudding user response:

because you're updating the current state when you emit WaitingAuth()

note the WaitingAuth constructor you're not passing any arguments to the super constructor aka AuthState hence all the values in the new state will be null.

a quick fix is to save the current state before emitting the WaitingAuth()

final signUpState = state;
emit(const WaitingAuth());
try {
  final data =  await _apiService.signUp(
    email: signUpState.email ?? "",
    password: signUpState.password ?? "",
    firstName: signUpState.firstName ?? "",
    lastName: signUpState.lastName ?? "",
    genderUuid: signUpState.genderUuid ?? "",
    ageGroupUuid: signUpState.ageGroupUuid ?? "",
    countryUuid: signUpState.countryUuid ?? ""
  );
  // ...
}
  • Related