我使用 bloc 模式创建了一个 Flutter 应用程序。我有一个名为 AuthCredentials 的类来存储授权所需的所有凭据。当前的应用程序状态称为 SignUpOTPState 但我想访问 AuthNavConfirmRegisterPhone 并获取电话号码。如何动态访问变量和函数?
-AuthNavState-
abstract class AuthNavState extends Equatable {
final String route;
const AuthNavState(this.route);
}
class AuthNavLogin extends AuthNavState {
AuthNavLogin() : super("/login");
@override
List<Object> get props => [];
}
class AuthNavRegister extends AuthNavState {
AuthNavRegister() : super("/register");
@override
List<Object> get props => [];
}
class AuthNavConfirmRegisterPhone extends AuthNavState {
final User user;
AuthNavConfirmRegisterPhone(this.user) : super("/register/confirm/otp");
@override
List<Object> get props => [];
}
-SignUpOTPState-
class SignUpOTPState{
final String otp;
bool get isValidOTP =>otp.length==6;
final FormSubmissionStatus formStatus;
SignUpOTPState({
this.otp ='',
this.formStatus = const InitialFormStatus(),
});
SignUpOTPState copyWith({
String? otp,
FormSubmissionStatus? formStatus,
}){
return SignUpOTPState(
otp: otp ?? this.otp,
formStatus: formStatus ?? this.formStatus,
);
}
}
-SignUpOTPBloc-
class SignUpOTPBloc extends Bloc<SignUpOTPEvent,SignUpOTPState>{
final AuthRepository authRepo;
final AuthNavCubit authNavCubit;
// final AuthPayload authPayload;
SignUpOTPBloc({required this.authRepo,required this.authNavCubit}) : super(SignUpOTPState());
@override
Stream<SignUpOTPState> mapEventToState(SignUpOTPEvent event) async* {
if(event is SignUpOTPChanged){
yield state.copyWith(otp:event.otp);
}
else if(event is SignUpOTPSubmitted){
print("submitteddddd");
yield state.copyWith(formStatus:FormSubmitting());
try{
//THE LINE I GET THE ERROR
final AuthPayload authPayload=await authRepo.confirmSignUp(AuthOTPCredentials(phone:
(authNavCubit.state is AuthNavConfirmRegisterPhone)?authNavCubit.state.user.phone:"not in the
state ", otp:state.otp));
print("Authpayloadddd(): $authPayload");
yield state.copyWith(formStatus: SubmissionSuccess());
authNavCubit.showEmailVerification(authPayload);
// final credentials=authNavCubit.credentials;
// credentials.userId=userId;
// authNavCubit.launchSession(credentials);
}on Exception catch(e){
print("--------------ERROR--------------");
yield state.copyWith(formStatus: SubmissionFailed(e));
}
}
}
}