4
graphql_flutter
return LazyCacheMap
, built_value
deserializeWith
JSON String
, how to make them work together.
- I use
graphql_flutter
to fetch data, and response give the result data asLazyCacheMap
. - And using
built_value
for data model & data serialization, but sincedeserializeWith
working withJSON String
.
What’s the best way to work with them together ?
- Should I just convert
LazyCacheMap
‘s data of toString
and calldeserializeWith
? - should I use other serialization pubs ?
3
2 Answers
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.
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")),
),
],
);
}
),
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.
How did you solve this?
Jun 30, 2019 at 18:25
@GEPD, use LazyCacheMap.data
Jun 30, 2019 at 22:37
Thanks! you should write the answer in a new post
Jun 30, 2019 at 23:01
|