I currently have a function in a bloc which is calling my repository to grab data from my server using GraphQL. I am able to get the data back. However I am not able to return it to my bloc as the function exits out after grabbing the data.
Here is the code in my bloc....
Either<AuthFailure, GLoginData_login> failureOrSuccess;
failureOrSuccess = await loginWithUsernameAndPassword(username: state.username, password: state.password)
.whenComplete(() { <---------- This line is never triggered (why not?)
failureOrSuccess.fold((l) => null, (r) {
print('You have the data $r');
});
});
As you can see I am awaiting for the response from the forwardedCall.
Here is the code in my repository which the bloc calls....
abstract class LoginRepository<TData, TVars, TRequest extends OperationRequest<TData, dynamic>> {
Future<Either<AuthFailure, GLoginData_login>> loginWithUsernameAndPassword({
@required Username username,
@required Password password,
});
}
This repository is an abstract class where the method is implemented in the following class...
class LoginUser extends LoginRepository {
@override
Future<Either<AuthFailure, GLoginData_login>> loginWithUsernameAndPassword(
{@required Username username, @required Password password}) =>
_runQuery(username: username, password: password);
Future<Either<AuthFailure, GLoginData_login>> _runQuery(
{@required Username username, @required Password password}) async {
final loginReq = GLoginReq(
(b) => b
..vars.LoginInput.username = usernameStr
..vars.LoginInput.password = passwordStr,
);
try {
return await GetIt.instance<Client>().request(loginReq).listen((response) {
if (!response.loading && response.dataSource == DataSource.Link &&
response.data != null) {
GLoginData data = response.data;
GLoginData_login login = data.login;
return login;
}
if (response.linkException != null) {
return response.graphqlErrors;
}
}).asFuture(); <------ I am marking it as a future so that it can be passed back
} on Exception catch (e) {
// Will need a created AuthException depending on our API and requirements
if (e.toString() == 'ERROR_NO_ACCOUNT_MATCHES_DETAILS_GIVEN') {
return left(const AuthFailure.invalidUsernameAndPaswordCombination());
} else {
return left(const AuthFailure.serverError());
}
}
}
}
I do receive the data from the serverising the client response. However in my bloc I cannot do anything with it as it just jumps out of the bloc after the loginWithUsernameAndPassword function completes.
How can I use the return value in my bloc, the whenComplete value line isn't working.
Thanks for any help you can provide.
just do:
either.fold(
(l) => Something(),
(r) => Something(),
);
If you have a function that returns the values then put the l or r into the function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.