added lazy query support

fixed possible race condition of simultanous refetches of the same query
This commit is contained in:
Kingkor Roy Tirtho
2022-06-09 13:33:39 +06:00
parent 9bcdfc9db7
commit 547cf65dd8
4 changed files with 88 additions and 5 deletions
+29 -3
View File
@@ -1,6 +1,7 @@
import 'dart:math';
import 'package:example/another_component.dart';
import 'package:example/lazy_query.dart';
import 'package:example/query_with_external_data.dart';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/material.dart';
@@ -35,7 +36,9 @@ final successJob = QueryJob<String, void>(
final failedJob = QueryJob<String, void>(
queryKey: "failure",
task: (queryKey, _) => Future.value("[$queryKey] Failed for unknown reason"),
task: (queryKey, _) => Random().nextBool()
? Future.error("[$queryKey] Failed for unknown reason")
: Future.value("Success, you'll get slowly ${Random().nextInt(100)}!"),
);
class MyHomePage extends StatefulWidget {
@@ -90,8 +93,20 @@ class _MyHomePageState extends State<MyHomePage> {
job: failedJob,
externalData: null,
builder: (context, query) {
if (query.hasError) return Text(query.error);
return Text("Failure. You're a failure ${query.data}");
return Row(
children: [
if (query.hasError)
Text(
"${query.error}. Retrying: ${query.retryAttempts}"),
if (query.hasData)
Text(
"Success after ${query.retryAttempts}. Data: ${query.data}"),
ElevatedButton(
child: Text("Refetch ${query.queryKey}"),
onPressed: () => query.refetch(),
)
],
);
},
),
],
@@ -106,6 +121,17 @@ class _MyHomePageState extends State<MyHomePage> {
);
},
),
const SizedBox(height: 10),
ElevatedButton(
child: const Text("Non Enabled Query Example"),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const LazyQuery(),
),
);
},
),
const AnotherComponent(),
],
),