从函数返回一个流



我正在使用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),
authFailureOrSuccess: none(),
),
signInWithEmailAndPasswordPressed: () async* {
yield* _performActionOnAuthFacade(
_authFacade.signInWithEmailAndPassword);
});
}

这里出现错误

signInWithEmailAndPasswordPressed: () async* {
yield* _performActionOnAuthFacade(
_authFacade.signInWithEmailAndPassword);
});

参数类型'Stream Function()'不能是赋值给参数类型"SignInFormStates Function()"。

My__performActionOnAuthFacadefunction

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,
authFailureOrSuccess: none(),
);
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),
authFailureOrSuccessOption: none(),
);
},
// 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,
authFailureOrSuccessOption: none(),
);
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,
authFailureOrSuccessOption: none(),
);
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调用流函数,但一个空的voidCallback也许?

state = signInFormEvents.when(
emailChanged: (value) => state.copyWith(
emailAddress: EmailAddress(value),
authFailureOrSuccess: none(),
),
passwordChanged: (value) => state.copyWith(
password: Password(value),
authFailureOrSuccess: none(),
),
signInWithEmailAndPasswordPressed: () => () async* {  //so a SignInFormStates Function() calls your stream function
yield* _performActionOnAuthFacade(
_authFacade.signInWithEmailAndPassword);
});

但仍然有机会之后,它会给你一些其他的错误告诉你状态是预期类型SignInFormStates,你传递给它一个流函数,或者它实际上等待流完成并返回新的状态产生,唯一要做的就是尝试看看会发生什么

最新更新