问题描述
我正在使用 riverpod 进行状态管理。
class SignInStateNotifier extends StateNotifier<SignInFormStates> {
SignInStateNotifier(this._authFacade) : super(SignInFormStates.initial());
final IAuthFacade _authFacade;
void mapEventToState(SignInFormEvents signInFormEvents) {
state = signInFormEvents.when(
emailChanged: (value) => state.copyWith(
emailAddress: EmailAddress(value),authFailureOrSuccess: none(),),passwordChanged: (value) => state.copyWith(
password: Password(value),signInWithEmailAndPasswordpressed: () async* {
yield* _performActionOnAuthFacade(
_authFacade.signInWithEmailAndPassword);
});
}
signInWithEmailAndPasswordpressed: () async* {
yield* _performActionOnAuthFacade(
_authFacade.signInWithEmailAndPassword);
});
参数类型 'Stream Function()' 不能是 分配给参数类型“SignInFormStates Function()”。
我的 __performActionOnAuthFacade
函数
Stream<SignInFormStates> _performActionOnAuthFacade(
Future<Either<AuthFailure,Unit>> Function({
@required EmailAddress emailAddress,@required Password password,})
forwardCall,) async* {
Either<AuthFailure,Unit> failureOrSuccess;
if (state.emailAddress.isValid() && state.password.isValid()) {
yield state.copyWith(
isSubmitting: true,);
failureOrSuccess = await _authFacade.registerWithEmailAndPassword(
emailAddress: state.emailAddress,password: state.password);
}
yield state.copyWith(
isSubmitting: false,showErrorMessage: true,authFailureOrSuccess: optionOf(failureOrSuccess));
}
解决方法
如果您想使用 StateNotifier
执行此操作,而无需使用 Stream
来更改状态,您需要执行以下操作:
class SignInFormStateNotifier extends StateNotifier<SignInFormState> {
final IAuthFacade _authFacade;
SignInFormStateNotifier(this._authFacade) : super(SignInFormState.initial());
Future handleEvent(SignInFormEvent event) async {
event.map(
// email changed
emailChanged: (event) {
state = state.copyWith(
emailAddress: EmailAddress(event.email),authFailureOrSuccessOption: none(),);
},// password changed
passwordChanged: (event) {
state = state.copyWith(
password: Password(event.password),// register with email and password
registerWithEmailAndPassword: (event) async {
await _performActionWithEmailAndPassword(
_authFacade.registerWithEmailAndPassword,// sign in with email and password
signInWithEmailAndPassword: (event) async {
await _performActionWithEmailAndPassword(
_authFacade.signInWithEmailAndPassword,// sign in with Google
signInWithGoogle: (event) async {
state = state.copyWith(
isSubmitting: true,);
final result = await _authFacade.signInWithGoogle();
state = state.copyWith(
isSubmitting: false,authFailureOrSuccessOption: some(result),);
}
Future _performActionWithEmailAndPassword(
Future<Either<AuthFailure,Unit>> Function({
@required EmailAddress emailAddress,@required Password password,})
action,) async {
Either<AuthFailure,Unit> result;
final isEmailValid = state.emailAddress.isValid();
final isPasswordValid = state.password.isValid();
if (isEmailValid && isPasswordValid) {
state = state.copyWith(
isSubmitting: true,);
result = await action(
emailAddress: state.emailAddress,password: state.password,);
state = state.copyWith(
authFailureOrSuccessOption: some(result),);
}
state = state.copyWith(
isSubmitting: false,showErrorMessages: true,authFailureOrSuccessOption: optionOf(result),);
}
}
,
我不知道您的 SignInFormStates
类,但它不希望在 signInWithEmailAndPasswordPressed
中调用 Stream 函数,但可能是一个空的 voidCallback?
state = signInFormEvents.when(
emailChanged: (value) => state.copyWith(
emailAddress: EmailAddress(value),authFailureOrSuccess: none(),),passwordChanged: (value) => state.copyWith(
password: Password(value),signInWithEmailAndPasswordPressed: () => () async* { //so a SignInFormStates Function() calls your stream function
yield* _performActionOnAuthFacade(
_authFacade.signInWithEmailAndPassword);
});
但是在那之后它仍然有机会给你一些其他错误,告诉你状态期望是 SignInFormStates
类型并且你传递给它一个流函数,或者它实际上等待流完成并且返回产生的新状态,唯一要做的就是尝试看看会发生什么