Flutter:查询 AWS AppSync

Flutter: Querying AWS AppSync

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

我正在使用 graphql_flutter 包,但无法弄清楚如何使用它进行查询。

这是我的代码片段:

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 函数。你可以改变你的方法,比如 this:


 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);

           } 
    

您可以从 official documentation 阅读更多关于异步编程的信息。