Flutter 颤振:查询AWS AppSync
我最近开始使用AWS AppSync和Flatter。我很难把他们联系起来 我使用的是graphql_颤振软件包,还没有弄清楚如何使用它进行查询 下面是我的代码片段:Flutter 颤振:查询AWS AppSync,flutter,graphql,aws-appsync,Flutter,Graphql,Aws Appsync,我最近开始使用AWS AppSync和Flatter。我很难把他们联系起来 我使用的是graphql_颤振软件包,还没有弄清楚如何使用它进行查询 下面是我的代码片段: final HttpLink httpLink = HttpLink( uri: 'https://myapi.xyz.com/graphql', ); final AuthLink authLink = AuthLink(
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);
如何使整个过程正常工作?在使用异步方法时,需要使用未来函数。您可以更改您的方法,如:
未来的getRepositories(int numOfRepositories)异步{
最终HttpLink HttpLink=HttpLink(
uri:'https://myapi.xyz.com/graphql',
);
最终AuthLink AuthLink=AuthLink(
getToken:()async=>'Bearer${globals.token}',
);
最终链接=authLink.concat(httpLink);
GraphQLClient=GraphQLClient(link:link,cache:InMemoryCache());
QueryOptions query=QueryOptions(documentNode:gql(querys.getAll));
var result=wait client.query(查询);
}
您可以从中阅读有关异步编程的更多信息
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);
}