Flutter & GraphQL Returning Data in Stateful Widget

Viewed 28

Been building an app using Flutter and calling to GraphQL database. At the moment, I have just been accessing data by wrapping the following in a stateless widget:

return Query(
        options: QueryOptions(
          document: gql(exampleGraphQL),
        ),
        builder: (QueryResult result, {fetchMore, refetch}) {
          if (result.hasException) {
            return Text(result.exception.toString());
          }
          if (result.isLoading) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }

          var exampleData = result.data?['example'];

          return Scaffold(...

But now, I am attempting to make a sortable DataTable, and I need to access the data in a stateful widget (or access it in a simple class) that cannot return a scaffold and just returns the data I want to access. I have looked for hours and cannot find how to access GraphQl data without returning a widget. Any help would be greatly appreciated.

1 Answers

If you're not already, try using the graphql package directly:

https://pub.dev/packages/graphql

As far as I can tell, the Query widget you're using looks very analogous to the FutureBuilder widget. In the same that when you don't need to actually build a widget from the result of a HTTP/fetch web request, you simply use await or .then(...). So here, you can you can use the the qraphql client and simply await the query to get the data you need.

Related