feat: add support for keepPreviousData & examples regarding this
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query_hooks/fl_query_hooks.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
final todoJob = QueryJob.withVariableKey<Map, void>(
|
||||
preQueryKey: "todo",
|
||||
task: (queryKey, _) async {
|
||||
final res = await http.get(
|
||||
Uri.parse(
|
||||
"https://jsonplaceholder.typicode.com/todos/${getVariable(queryKey)}"),
|
||||
);
|
||||
return jsonDecode(res.body);
|
||||
},
|
||||
keepPreviousData: true,
|
||||
);
|
||||
|
||||
class QueryHookPreviousDataExample extends HookWidget {
|
||||
const QueryHookPreviousDataExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final id = useState(1);
|
||||
final query =
|
||||
useQuery(job: todoJob(id.value.toString()), externalData: null);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Query Variable Key with keepPreviousData",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
if (query.hasError)
|
||||
Text(query.error.toString())
|
||||
else if (!query.hasData)
|
||||
const CircularProgressIndicator()
|
||||
else
|
||||
Text(jsonEncode(query.data ?? {})),
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.remove),
|
||||
onPressed: () {
|
||||
id.value -= 1;
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
id.value += 1;
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import 'package:fl_query_hooks_example/components/basic_hook_query.dart';
|
||||
import 'package:fl_query_hooks_example/components/lazy_hook_query.dart';
|
||||
import 'package:fl_query_hooks_example/components/mutation_hook_variable_key.dart';
|
||||
import 'package:fl_query_hooks_example/components/query_hook_external_data.dart';
|
||||
import 'package:fl_query_hooks_example/components/query_hook_previous_data.dart';
|
||||
import 'package:fl_query_hooks_example/components/query_hook_variable_key.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -53,6 +54,7 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
|
||||
QueryHookExternalDataExample(),
|
||||
LazyHookQueryExample(),
|
||||
QueryHookVariableKeyExample(),
|
||||
QueryHookPreviousDataExample(),
|
||||
Divider(),
|
||||
BasicHookMutationExample(),
|
||||
MutationHookVariableKeyExample(),
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// ignore_for_file: invalid_use_of_protected_member
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query_hooks/src/utils.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
@@ -31,10 +33,11 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
|
||||
final oldOnData = usePrevious(onData);
|
||||
final oldOnError = usePrevious(onError);
|
||||
|
||||
final init = useCallback(() {
|
||||
final init = useCallback(([T? previousData]) {
|
||||
query.value = queryBowl.addQuery<T, Outside>(
|
||||
job,
|
||||
externalData: externalData,
|
||||
previousData: previousData,
|
||||
key: uKey,
|
||||
onData: onData,
|
||||
onError: onError,
|
||||
@@ -66,7 +69,17 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
|
||||
final hasOnDataChanged = oldOnData != onData && oldOnData != null;
|
||||
if (oldJob != null && oldJob.queryKey != job.queryKey) {
|
||||
disposeQuery();
|
||||
init();
|
||||
|
||||
/// setting the new query's initial data as prev query's data
|
||||
/// when [job.keepPreviousData] is true and both are dynamic
|
||||
if (oldJob.isDynamic &&
|
||||
job.isDynamic &&
|
||||
oldJob.keepPreviousData == true &&
|
||||
job.keepPreviousData == true) {
|
||||
init(query.value.data);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
} else if (oldExternalData != null &&
|
||||
externalData != null &&
|
||||
!isShallowEqual(oldExternalData, externalData)) {
|
||||
|
||||
Reference in New Issue
Block a user