我正在按照教程here在项目中使用flutter-graphql。我有使用Laravel graphql-laravel构建的api。我想让所有用户都从api显示在我的flutter项目中,并且遇到问题FormatException, FormatException: Unexpected character (at character 1
。我的api和我正在关注的教程的数据响应是相同的。以下是我的代码:
Main.dart
import 'package:flutter/material.dart';
...
GraphQLConfiguration graphQLConfiguration = GraphQLConfiguration();
void main() => runApp(
GraphQLProvider(
client: graphQLConfiguration.client,
child: CacheProvider(child: MyApp()),
),
);
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
void getUsers() async {
QueryMutation queryMutation = QueryMutation();
GraphQLClient _client = graphQLConfiguration.clientToQuery();
QueryResult result = await _client.query(
QueryOptions(
document: queryMutation.getUsers(),
),
);
print(result);
}
}
@override
void initState() {
super.initState();
getUsers();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'GraphQL',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home:body: Text('Hello'),
),
);
}
}
GraphQLConf.dart
import "package:flutter/material.dart";
import "package:graphql_flutter/graphql_flutter.dart";
class GraphQLConfiguration {
static HttpLink httpLink = HttpLink(
uri: "http://127.0.0.1/julin/api",
headers: {
'accept':'*/*',
'Content-Type': 'application/json;charset=UTF-8',
'Charset': 'utf-8'
}
);
ValueNotifier<GraphQLClient> client = ValueNotifier(
GraphQLClient(
link: httpLink,
cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
),
);
GraphQLClient clientToQuery() {
return GraphQLClient(
cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
link: httpLink,
);
}
}
QueryMutation.dart
class QueryMutation {
String getUsers() {
return """
{
users {
id
name
email
}
}
""";
}
}
我整天都在尝试通过遵循Stackoverflow和google的答案来解决此问题,但是现在真的卡住了,不知道要解决这个问题。请具有GraphQL和graphql Laravel经验的每个人都对我有所帮助。 非常感谢你。