我正在將我的專案遷移到新版本的 Flutter 并使用 BloC 包。如您所知,Bloc 已經更新并且它已經更改了大部分代碼,所以現在我正在調整我的,但我有一些錯誤。
引數型別“Future Function(bool)”不能分配給引數型別“UserState Function(bool)”
主體可能正常完成,導致回傳“null”,但回傳型別可能是不可為空的型別。
前 :
Stream<UserState> _validate(ValidateEvent event) async* {
final successOrFailure = await services.validate();
yield* successOrFailure.fold(
(sucess) async* {
final session = await services.getSession();
yield* session.fold(
(session) async* {
yield UserSuccess(session);
yield UserLogged(session);
},
(failure) async* {
yield UserError(failure.message);
},
);
},
(failure) async* {
yield UserError(failure.message);
},
);
}
后 :
UserBloc({this.services}) : super(UserInitial()) {
on<ValidateEvent>(_onValidate);
}
void _onValidate(
ValidateEvent event,
Emitter<UserState> emit,
) async {
final successOrFailure = await services.validate();
emit(
successOrFailure.fold(
(success) async {
final session = await services.getSession();
emit(
session.fold(
(session) { // ! The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
emit(UserSuccess(session));
emit(UserLogged(session));
},
(failure) { // ! The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
emit(UserError(failure.message));
},
),
);
}, // ! The argument type 'Future<Null> Function(bool)' can't be assigned to the parameter type 'UserState Function(bool)
(failure) { // ! The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
emit(UserError(failure.message));
},
),
);
}
我需要在里面有那個異步,因為我再次呼叫了服務,關于身體,在我看來我必須以某種方式回傳它,但我里面有一些邏輯,我不知道該怎么做。
uj5u.com熱心網友回復:
在這種情況下,您唯一可以emit做的是您的狀態型別的實體UserState。與其嘗試發出 Future,不如使用await它。
void _onValidate(
ValidateEvent event,
Emitter<UserState> emit,
) async {
final successOrFailure = await services.validate();
await successOrFailure.fold( // This will return FutureOr<Null>
(success) async {
final session = await services.getSession();
session.fold( // This isn't asynchronous, so no need to await here
(session) {
emit(UserSuccess(session));
emit(UserLogged(session));
},
(failure) {
emit(UserError(failure.message));
},
);
},
(failure) {
emit(UserError(failure.message));
},
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/420871.html
標籤:
上一篇:帶有飛鏢的http請求
