如何使graphql_flutter的LazyCacheMap和built_value的deserializeWith JSON字符串一起工作?

4

graphql_flutter 返回 LazyCacheMapbuilt_value 使用 JSON字符串 进行数据模型和数据序列化,如何使它们协同工作。

  1. 我使用 graphql_flutter 获取数据,响应将结果数据返回为 LazyCacheMap
  2. 并且使用 built_value 作为数据模型和数据序列化,但是由于 deserializeWith 只能处理 JSON字符串

最佳的解决方案是什么?

  • 我是否应该先将 LazyCacheMap 的数据转换为 String 并调用 deserializeWith
  • 还是应该使用其他序列化库?

你是怎么解决这个问题的? - GEPD
1
@GEPD,请使用 LazyCacheMap.data。 - JerryZhou
谢谢!您应该在新帖中编写答案。 - GEPD
2个回答

0

首先使用 dart:convert 包将响应中的 LazyCacheMap 编码为 JSON,然后执行您想要的任何操作。

import 'dart:convert';

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

print(jsonEncode(result.data));

对我有用。


0
我通过创建一个懒惰的查询类来解决这个问题。
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,
    );   ;
    }

  }
}

使用方法

 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")),
                        ),
                        ],
                        );
                      }

                  ),

你的回答可以通过提供更多支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人能够确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接