flutter 如何修复在扑动/阻塞中添加另一个发射时缺少状态?

6za6bjd0  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(143)

我是flutter的新手,试图找出块是如何工作的...我有一个状态,当我试图发出WaitingAuth(当我发送注册请求时,这是CircularProgressIndicator)时,我的状态消失了...
这个emit(WaitingAuth())我在发送请求时插入,服务器返回400错误,请求主体看起来像这样:
电子邮件:"",密码:""等等..
但是如果我删除这个emit(WaitingAuth()),那么一切都正常工作,状态看起来像这样:
email: "dfdf@gmail.com", password: "dfdsfdffsd", and so on..
我不明白为什么我的状态消失了...
这是我的肘档:

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

这是我的州档桉

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();
}
5rgfhyps

5rgfhyps1#

因为当你发出WaitingAuth()时你正在更新当前状态
注意WaitingAuth构造函数,你没有传递任何参数给超级构造函数,也就是AuthState,因此新状态中的所有值都将为空。
一个快速解决方案是在发出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 ?? ""
  );
  // ...
}

相关问题