Skip to content

Instantly share code, notes, and snippets.

@vinceramcesoliveros
Last active April 19, 2020 04:08
Show Gist options
  • Save vinceramcesoliveros/790c42b44ccb5afc9721467e8f7e0ec4 to your computer and use it in GitHub Desktop.
Save vinceramcesoliveros/790c42b44ccb5afc9721467e8f7e0ec4 to your computer and use it in GitHub Desktop.
Flutter Graphql basic query example
import 'package:flutter/material.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
final HttpLink httpLink = HttpLink(
uri: 'https://countries.trevorblades.com/',
);
final ValueNotifier<GraphQLClient> client = ValueNotifier<GraphQLClient>(
GraphQLClient(
link: httpLink,
cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
),
);
runApp(
MaterialApp(
home: GraphQLProvider(
client: client,
child: MyApp(),
),
),
);
}
class MyApp extends StatefulWidget {
const MyApp();
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Query(
options: QueryOptions(
documentNode: gql(r"""
query{
continents{
name
}"""),
),
builder: (
QueryResult result, {
VoidCallback refetch,
FetchMore fetchMore,
}) {
if (result.hasException) {
return Text(result.exception.toString());
}
if (result.loading) {
return Center(child: CircularProgressIndicator());
}
print(result.data);
return ListView.builder(
itemBuilder: (context, index) => Text("${result.data}"),
);
}),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment