graphql_flutter return LazyCacheMap, built_value deserializeWith JSON String, how to make them work together

graphql_flutter return LazyCacheMap, built_value deserializeWith JSON String, how to make them work together


4

graphql_flutter return LazyCacheMap, built_value deserializeWith JSON String, how to make them work together.

  1. I use graphql_flutter to fetch data, and response give the result data as LazyCacheMap.
  2. And using built_value for data model & data serialization, but since deserializeWith working with JSON String.

What’s the best way to work with them together ?

  • Should I just convert LazyCacheMap‘s data of to String and call deserializeWith ?
  • should I use other serialization pubs ?

Share

3

  • How did you solve this?

    – GEPD

    Jun 30, 2019 at 18:25

  • 1

    @GEPD, use LazyCacheMap.data

    – JerryZhou

    Jun 30, 2019 at 22:37

  • Thanks! you should write the answer in a new post

    – GEPD

    Jun 30, 2019 at 23:01

2 Answers
2

Reset to default


0

First encode the response LazyCacheMap to JSON using the dart:convert package, then perform whatever operation you want.

import 'dart:convert';

GraphQLClient _client = graphQLConfiguration.clientToQuery();
QueryResult result = await _client.query(
  QueryOptions(
    documentNode: gql(GraphQlQueries.authenticateUser()),
  )
);

print(jsonEncode(result.data));

Worked for me.

Share


0

I solve by creating the lazy Query class.

import 'package:flutter/cupertino.dart';
import 'package:graphql_flutter/graphql_flutter.dart';

class LazyQuery<TParsed> extends StatefulWidget {
  const LazyQuery ({
    final Key? key,
    required this.options,
    required this.builder,
  });

  final QueryOptions<TParsed> options;
  final QueryBuilder<TParsed> builder; 


  @override
  State<LazyQuery> createState() => _LazyQueryState();
}

class _LazyQueryState extends State<LazyQuery> {

  bool initialFetch = false;
  final QueryResult<String> result= QueryResult.unexecuted;

  get fetchMore => null;
  Future<QueryResult<Object?>> refetch(){
   setState(() {
      initialFetch =true;
   });
   return  Future(() =>QueryResult.unexecuted);
 }
  @override
  Widget build(BuildContext context) {

    if(initialFetch) {
      return   Query(options: widget.options, builder: widget.builder);
    } else {

    return  widget.builder(
      result ,
      fetchMore:  fetchMore,
      refetch: refetch,
    );   ;
    }

  }
}

usage

 LazyQuery(
                      options: QueryOptions(
                          variables: {
                            "email":email,
                            "password":password,
                          },
                          fetchPolicy: FetchPolicy.networkOnly,
                          document:   loginQuery ),
                      builder: (QueryResult result, {fetchMore, refetch}) {
                        if (result.data != null) {
                         moveToHome(result.data?["login"]);

                        }
                        return  result.isLoading
                            ?     const LoadingIndicator(
                          indicatorType: Indicator.ballPulseSync,  // Required, The loading type of the widget
                          colors: [Colors.green],
                        )
                            : Column(
                        children: [
                        Row( mainAxisAlignment:MainAxisAlignment.end,
                        children: [TextButton(onPressed:(){


                        }, child: const Text("Forget password"))],),
                        const SizedBox(height: 16,),
                        Center(
                        child:     ElevatedButton(onPressed: () async {
                           refetch!();
                        }, child: const Text("Login")),
                        ),
                        ],
                        );
                      }

                  ),

Share



Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *