Don*_*k3y 3 asynchronous async-await dart flutter
Future<Either<Failure, User>> call(SignUpParams params) async {
Either<Failure, User> failureOrUser;
// Creates a User
failureOrUser = await repository.signUpUser(
params.email, params.password, params.type);
// Creates a [Learner / Instructor] if User returned
await failureOrUser.fold(
(failure) => left(failure),
(user) async {
final remoteServerFailureOrSuccess =
await createLearnerOrInstructor(CreateLOIParam(user: user));
// check if [Learner / Instructor] creation has failed
remoteServerFailureOrSuccess.fold(
(failure) => failureOrUser = left(failure),
(success) => null
);
}
);
return failureOrUser;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我需要在方法前面 放置一个等待failureOrUser.fold();
。
如果我不这样做,那么该(user) async {}
方法不会等待
final remoteServerFailureOrSuccess = await createLearnerOrInstructor(CreateLOIParam(user: user));
Run Code Online (Sandbox Code Playgroud)
并且return failureOrUser;
之前被调用
remoteServerFailureOrSuccess.fold(
(failure) => failureOrUser = left(failure),
(success) => null
);
Run Code Online (Sandbox Code Playgroud)
叫做。
我收到一个“在“对象”之前应用等待,这不是未来”错误提示,但代码仅等到整个方法完成后才返回,以这种方式完成。
我试过把
await remoteServerFailureOrSuccess.fold(
(failure) => failureOrUser = left(failure),
(success) => null
);
Run Code Online (Sandbox Code Playgroud)
但这仍然行不通。
所以对我来说似乎
final remoteServerFailureOrSuccess = await createLearnerOrInstructor(CreateLOIParam(user: user));
Run Code Online (Sandbox Code Playgroud)
实际上并没有在等待。
好吧,我想我明白了,因为我将(user) async {}
其设置为“未来”,但另一半不是异步的(failure) => left(failure)
,所以我认为我因此而收到错误。
一旦我将故障设置为异步,错误就消失了。
(failure) async => left(failure)
这是一个有点愚蠢的问题,但我会保留它,以防有人遇到同样的错误。
归档时间: |
|
查看次数: |
1927 次 |
最近记录: |