简体   繁体   English

使用 Flutter 和 Ferry 获取数据后,无法使用来自 GraphQl 查询的数据

[英]Cannot use data from GraphQl query after getting data using Flutter and Ferry

I currently have a function in a bloc which is calling my repository to grab data from my server using GraphQL.我目前在一个块中有一个 function,它正在调用我的存储库以使用 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.但是,由于 function 在获取数据后退出,我无法将其返回到我的集团。

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.如您所见,我正在等待 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,其中该方法在以下 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.但是,在我的集团中,我无法对它做任何事情,因为它只是在 loginWithUsernameAndPassword function 完成后跳出集团。

How can I use the return value in my bloc, the whenComplete value line isn't working.如何在我的块中使用返回值,whenComplete 值行不起作用。

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.如果您有一个返回值的 function,则将 l 或 r 放入 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM