繁体   English   中英

Flutter:查询 AWS AppSync

[英]Flutter: Querying AWS AppSync

我最近开始使用 AWS AppSync 和 Flutter。我很难将它们相互连接起来。

我正在使用 graphql_flutter package 并且无法弄清楚如何将它用于查询目的。

这是我的代码片段:

final HttpLink httpLink = HttpLink(
              uri: 'https://myapi.xyz.com/graphql',
            );


            final AuthLink authLink = AuthLink(
              getToken: () async => 'Bearer ${globals.token}',  
            );

            final Link link = authLink.concat(httpLink);

            GraphQLClient client = GraphQLClient(link: link, cache: InMemoryCache());

            QueryOptions query = QueryOptions(documentNode: gql(queries.getAll));

            var result = await client.query(query);

我收到以下错误:

Error: 'await' can only be used in 'async' or 'async*' methods.
            var result = await client.query(query);  

我怎样才能使整个事情正常进行?

在使用异步方法时,您需要使用 Future function。 您可以像这样更改方法:


 Future<QueryResult> getRepositories(int numOfRepositories) async {
            final HttpLink httpLink = HttpLink(
              uri: 'https://myapi.xyz.com/graphql',
            );


            final AuthLink authLink = AuthLink(
              getToken: () async => 'Bearer ${globals.token}',  
            );

            final Link link = authLink.concat(httpLink);

            GraphQLClient client = GraphQLClient(link: link, cache: InMemoryCache());

            QueryOptions query = QueryOptions(documentNode: gql(queries.getAll));

            var result = await client.query(query);

           } 
    

您可以从官方文档中阅读有关异步编程的更多信息。

暂无
暂无

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

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