feat: clean old junk
This commit is contained in:
@@ -1,97 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
final infiniteQueryJob = InfiniteQueryJob<Map, void, int>(
|
||||
queryKey: "infinite-posts",
|
||||
initialParam: 1,
|
||||
task: (queryKey, pageParam, externalData) async {
|
||||
return jsonDecode(
|
||||
(await http.get(
|
||||
Uri.parse("https://jsonplaceholder.typicode.com/posts/$pageParam"),
|
||||
))
|
||||
.body,
|
||||
);
|
||||
},
|
||||
getNextPageParam: (lastPage, lastParam) {
|
||||
return lastParam + 1;
|
||||
},
|
||||
getPreviousPageParam: (lastPage, lastParam) {
|
||||
return lastParam - 1;
|
||||
},
|
||||
);
|
||||
|
||||
class BasicInfiniteQueryExample extends StatelessWidget {
|
||||
const BasicInfiniteQueryExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: InfiniteQueryBuilder<Map, void, int>(
|
||||
job: infiniteQueryJob,
|
||||
externalData: null,
|
||||
builder: (context, infiniteQuery) {
|
||||
return Stack(
|
||||
children: [
|
||||
ListView.builder(
|
||||
itemCount: infiniteQuery.pages.length + 1,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0) {
|
||||
return Text(
|
||||
"""
|
||||
InfiniteQuery properties
|
||||
|
||||
isFetchingNextPage: ${infiniteQuery.isFetchingNextPage}
|
||||
isFetchingPreviousPage: ${infiniteQuery.isFetchingPreviousPage}
|
||||
isLoading: ${infiniteQuery.isLoading}
|
||||
isRefetching: ${infiniteQuery.isRefetching}
|
||||
isError: ${infiniteQuery.isError}
|
||||
isSuccess: ${infiniteQuery.isSuccess}
|
||||
isIdle: ${infiniteQuery.isIdle}
|
||||
isInactive: ${infiniteQuery.isInactive}
|
||||
isStale: ${infiniteQuery.isStale}
|
||||
fetched: ${infiniteQuery.fetched}
|
||||
|
||||
hasData: ${infiniteQuery.hasData}
|
||||
hasError: ${infiniteQuery.hasError}
|
||||
hasNextPage: ${infiniteQuery.hasNextPage}
|
||||
hasPreviousPage: ${infiniteQuery.hasPreviousPage}
|
||||
|
||||
refetchCount: ${infiniteQuery.refetchCount}
|
||||
retryAttempts: ${infiniteQuery.retryAttempts}
|
||||
updatedAt: ${infiniteQuery.updatedAt}
|
||||
""",
|
||||
);
|
||||
}
|
||||
final page = infiniteQuery.pages[index - 1];
|
||||
return ListTile(
|
||||
title: Text(page?["title"] ?? ""),
|
||||
subtitle: Text(page?["body"] ?? ""),
|
||||
);
|
||||
},
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.bottomRight,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh_rounded),
|
||||
onPressed: () => infiniteQuery.refetchPages(),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.get_app_rounded),
|
||||
onPressed: () => infiniteQuery.fetchNextPage(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_query_example/components/basic_query.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final basicMutationJob = MutationJob<Map, Map<String, dynamic>>(
|
||||
mutationKey: "basic-mutation-example",
|
||||
task: (key, data) async {
|
||||
final response = await http.post(
|
||||
Uri.parse(
|
||||
// to simulate a failing response environment
|
||||
Random().nextBool()
|
||||
? "https://jsonplaceholder.typicode.com/posts"
|
||||
: "https://google.com",
|
||||
),
|
||||
headers: {'Content-type': 'application/json; charset=UTF-8'},
|
||||
body: jsonEncode(data),
|
||||
);
|
||||
return jsonDecode(response.body);
|
||||
},
|
||||
);
|
||||
|
||||
class BasicMutationExample extends StatefulWidget {
|
||||
const BasicMutationExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<BasicMutationExample> createState() => _BasicMutationExampleState();
|
||||
}
|
||||
|
||||
class _BasicMutationExampleState extends State<BasicMutationExample> {
|
||||
late TextEditingController titleController;
|
||||
late TextEditingController bodyController;
|
||||
late int id;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
id = Random().nextInt(2000000);
|
||||
titleController = TextEditingController();
|
||||
bodyController = TextEditingController();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
titleController.dispose();
|
||||
bodyController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Basic Mutation Example (with Failure & Retry simulation)",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
MutationBuilder<Map, Map<String, dynamic>>(
|
||||
job: basicMutationJob,
|
||||
onMutate: (v) {
|
||||
final data =
|
||||
QueryBowl.of(context).getQuery(successJob.queryKey)?.data;
|
||||
QueryBowl.of(context)
|
||||
.setQueryData<String, void>(successJob.queryKey, (oldData) {
|
||||
if (oldData?.contains("After Mutate (OPTIMISTIC UPDATE)") ==
|
||||
true) {
|
||||
return "$oldData";
|
||||
}
|
||||
return "$oldData - After Mutate (OPTIMISTIC UPDATE)";
|
||||
});
|
||||
return data;
|
||||
},
|
||||
onData: (data, variables, context) {
|
||||
print("Passed Variable: $variables");
|
||||
print("Safe Previous Value: $context");
|
||||
},
|
||||
builder: (context, mutation) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: titleController,
|
||||
decoration: const InputDecoration(labelText: "Title"),
|
||||
),
|
||||
TextField(
|
||||
controller: bodyController,
|
||||
decoration: const InputDecoration(labelText: "Body"),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
final title = titleController.value.text;
|
||||
final body = bodyController.value.text;
|
||||
if (body.isEmpty || title.isEmpty) return;
|
||||
mutation.mutate({
|
||||
"title": title,
|
||||
"body": body,
|
||||
"id": id,
|
||||
}, onData: (data, variables, context) {
|
||||
// resetting the form
|
||||
titleController.text = "";
|
||||
bodyController.text = "";
|
||||
});
|
||||
},
|
||||
child: const Text("Post"),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
if (mutation.hasData) Text("Response\n${mutation.data}"),
|
||||
if (mutation.hasError) Text(mutation.error.toString()),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final bowl = QueryBowl();
|
||||
|
||||
final successJob = QueryJob<String, void>(
|
||||
queryKey: "greetings-example",
|
||||
task: (queryKey, _) => Future.delayed(
|
||||
const Duration(seconds: 2),
|
||||
() =>
|
||||
"The work successfully executed. Data: key=($queryKey) value=${Random.secure().nextInt(100)}",
|
||||
),
|
||||
);
|
||||
|
||||
final canFailJob = QueryJob<String, void>(
|
||||
queryKey: "failure-example",
|
||||
task: (queryKey, _) => Random().nextBool()
|
||||
? Future.error("$queryKey operation failed for unknown reason")
|
||||
: Future.value(
|
||||
"Successful execution. Result: $queryKey=${Random().nextInt(100)}",
|
||||
),
|
||||
);
|
||||
|
||||
class BasicQueryExample extends StatelessWidget {
|
||||
const BasicQueryExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Basic Query Example",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
QueryBuilder<String, void>(
|
||||
job: successJob,
|
||||
externalData: null,
|
||||
onData: (data) {
|
||||
print("Success: $data");
|
||||
},
|
||||
builder: (context, query) {
|
||||
if (!query.hasData || query.isLoading || query.isRefetching) {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
return Row(
|
||||
children: [
|
||||
Text(query.data!),
|
||||
ElevatedButton(
|
||||
child: const Text("Refetch"),
|
||||
onPressed: () async {
|
||||
await query.refetch();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
QueryBuilder<String, void>(
|
||||
job: canFailJob,
|
||||
externalData: null,
|
||||
onError: (error) {
|
||||
print("Error: $error");
|
||||
},
|
||||
builder: (context, query) {
|
||||
if (!query.hasData || query.isLoading || query.isRefetching) {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
return Row(
|
||||
children: [
|
||||
if (query.hasError)
|
||||
Text(
|
||||
"${query.error}. Retrying: ${query.retryAttempts}",
|
||||
),
|
||||
if (query.hasData)
|
||||
Text(
|
||||
"Success after ${query.retryAttempts}\nData: ${query.data}",
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text("Refetch"),
|
||||
onPressed: () async {
|
||||
await query.refetch();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class Todo {
|
||||
int? userId;
|
||||
int? id;
|
||||
String? title;
|
||||
bool? completed;
|
||||
|
||||
Todo({this.userId, this.id, this.title, this.completed});
|
||||
|
||||
Todo.fromJson(Map<String, dynamic> json) {
|
||||
userId = json['userId'];
|
||||
id = json['id'];
|
||||
title = json['title'];
|
||||
completed = json['completed'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = {};
|
||||
data['userId'] = userId;
|
||||
data['id'] = id;
|
||||
data['title'] = title;
|
||||
data['completed'] = completed;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
final infiniteQueryDiskCacheExampleQuery =
|
||||
InfiniteQueryJob<List<Todo>, void, int>(
|
||||
queryKey: 'infiniteQueryDiskCacheExampleQuery',
|
||||
initialParam: 0,
|
||||
getNextPageParam: (lastPage, lastParam) {
|
||||
if (lastPage.length < 5) return null;
|
||||
return lastParam + 5;
|
||||
},
|
||||
getPreviousPageParam: (firstPage, firstParam) {
|
||||
if (firstParam == 0) return null;
|
||||
return firstParam - 5;
|
||||
},
|
||||
serialize: (data) {
|
||||
return jsonEncode(data.map((todo) => todo.toJson()).toList());
|
||||
},
|
||||
deserialize: (raw) {
|
||||
return List.from(jsonDecode(raw))
|
||||
.map((todo) => Todo.fromJson(todo))
|
||||
.toList();
|
||||
},
|
||||
serializePageParam: (param) => param.toString(),
|
||||
deserializePageParam: (rawParam) => int.parse(rawParam),
|
||||
task: (_, pageParam, __) async {
|
||||
final res = await http.get(
|
||||
Uri.parse(
|
||||
"https://jsonplaceholder.typicode.com/todos?_start=$pageParam&_end=${pageParam + 5}"),
|
||||
);
|
||||
final body = List.from(jsonDecode(res.body))
|
||||
.map((todo) => Todo.fromJson(todo))
|
||||
.toList()
|
||||
..shuffle();
|
||||
if (pageParam == 0) await Future.delayed(const Duration(seconds: 5));
|
||||
return body;
|
||||
},
|
||||
);
|
||||
|
||||
class InfiniteQueryDiskCacheExample extends StatefulWidget {
|
||||
const InfiniteQueryDiskCacheExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<InfiniteQueryDiskCacheExample> createState() =>
|
||||
_InfiniteQueryDiskCacheExampleState();
|
||||
}
|
||||
|
||||
class _InfiniteQueryDiskCacheExampleState
|
||||
extends State<InfiniteQueryDiskCacheExample> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
title: const Text("Infinite Query Disk Cache Example"),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Infinite Query Disk Cache Example")),
|
||||
body: InfiniteQueryBuilder<List<Todo>, void, int>(
|
||||
job: infiniteQueryDiskCacheExampleQuery,
|
||||
externalData: null,
|
||||
builder: (context, query) {
|
||||
final data = query.pages
|
||||
.expand((page) => page?.toList() ?? <Todo>[])
|
||||
.toList();
|
||||
return Scaffold(
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: const Icon(Icons.download_rounded),
|
||||
onPressed: () {
|
||||
if (query.hasNextPage == true) {
|
||||
query.fetchNextPage();
|
||||
}
|
||||
},
|
||||
),
|
||||
body: ListView.builder(
|
||||
itemCount: data.length,
|
||||
itemBuilder: (context, index) {
|
||||
return CheckboxListTile(
|
||||
value: data[index].completed == true,
|
||||
title: Text(data[index].title ?? ""),
|
||||
dense: true,
|
||||
secondary: Text(data[index].id.toString()),
|
||||
onChanged: null,
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final lazyQueryJob = QueryJob<String, String>(
|
||||
queryKey: "lazy-query",
|
||||
enabled: false,
|
||||
task: (
|
||||
queryKey,
|
||||
data,
|
||||
) {
|
||||
return Future.delayed(const Duration(milliseconds: 500),
|
||||
() => "Result: key=$queryKey value=$data");
|
||||
},
|
||||
);
|
||||
|
||||
class LazyQueryExample extends StatelessWidget {
|
||||
const LazyQueryExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Lazy Query Example",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
QueryBuilder<String, String>(
|
||||
job: lazyQueryJob,
|
||||
externalData: "Love",
|
||||
builder: (context, query) {
|
||||
return Row(
|
||||
children: [
|
||||
Text("Current Data: ${query.data ?? "Loading"}"),
|
||||
ElevatedButton(
|
||||
child: const Text("Refetch Query"),
|
||||
onPressed: () => query.refetch(),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final mutationVariableKeyJob = MutationJob.withVariableKey<String, double>(
|
||||
preMutationKey: "mutation-example",
|
||||
task: (queryKey, variables) {
|
||||
return Future.value("$variables");
|
||||
},
|
||||
);
|
||||
|
||||
class MutationVariableKeyExample extends StatefulWidget {
|
||||
const MutationVariableKeyExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<MutationVariableKeyExample> createState() =>
|
||||
_MutationVariableKeyExampleState();
|
||||
}
|
||||
|
||||
class _MutationVariableKeyExampleState
|
||||
extends State<MutationVariableKeyExample> {
|
||||
late double id;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
id = Random().nextDouble();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Mutation Variable Key Example",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
MutationBuilder<String, double>(
|
||||
job: mutationVariableKeyJob(id.toString()),
|
||||
builder: (context, mutation) {
|
||||
return Row(
|
||||
children: [
|
||||
Text("${mutation.mutationKey} Result: ${mutation.data}"),
|
||||
ElevatedButton(
|
||||
child: const Text("Generate Random Data"),
|
||||
onPressed: () {
|
||||
mutation.mutate(Random().nextDouble());
|
||||
},
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text("New Mutation"),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
id = Random().nextDouble();
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
final queryDiskCacheExampleQuery = QueryJob<List<User>, void>(
|
||||
queryKey: 'queryDiskCacheExampleQuery',
|
||||
serialize: (data) {
|
||||
return jsonEncode(data.map((user) => user.toJson()).toList());
|
||||
},
|
||||
deserialize: (raw) {
|
||||
return List.from(jsonDecode(raw))
|
||||
.map((user) => User.fromJson(user))
|
||||
.toList();
|
||||
},
|
||||
task: (_, __) async {
|
||||
final res =
|
||||
await http.get(Uri.parse("https://jsonplaceholder.typicode.com/users"));
|
||||
final body = List.from(jsonDecode(res.body))
|
||||
.map((user) => User.fromJson(user))
|
||||
.toList()
|
||||
..shuffle();
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
return body;
|
||||
},
|
||||
);
|
||||
|
||||
class User {
|
||||
int? id;
|
||||
String? name;
|
||||
String? username;
|
||||
String? email;
|
||||
|
||||
User({this.id, this.name, this.username, this.email});
|
||||
|
||||
User.fromJson(Map<String, dynamic> json) {
|
||||
id = json['id'];
|
||||
name = json['name'];
|
||||
username = json['username'];
|
||||
email = json['email'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = {};
|
||||
data['id'] = id;
|
||||
data['name'] = name;
|
||||
data['username'] = username;
|
||||
data['email'] = email;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class QueryDiskCacheExample extends StatelessWidget {
|
||||
const QueryDiskCacheExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return QueryBuilder<List<User>, void>(
|
||||
job: queryDiskCacheExampleQuery,
|
||||
externalData: null,
|
||||
builder: (context, query) {
|
||||
if (!query.hasData) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: query.data?.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(query.data![index].name!),
|
||||
subtitle: Text(query.data![index].email!),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final jobWithExternalData = QueryJob<String, double>(
|
||||
queryKey: "query-external-data",
|
||||
cacheTime: const Duration(seconds: 10),
|
||||
refetchOnExternalDataChange: true,
|
||||
task: (queryKey, data) {
|
||||
return Future.delayed(const Duration(milliseconds: 500),
|
||||
() => "Hello from $queryKey with $data");
|
||||
},
|
||||
);
|
||||
|
||||
class QueryExternalDataExample extends StatefulWidget {
|
||||
const QueryExternalDataExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<QueryExternalDataExample> createState() =>
|
||||
_QueryExternalDataExampleState();
|
||||
}
|
||||
|
||||
class _QueryExternalDataExampleState extends State<QueryExternalDataExample> {
|
||||
late double externalData;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
externalData = Random().nextDouble() * 200;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Query With External Data Example",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
QueryBuilder<String, double>(
|
||||
job: jobWithExternalData,
|
||||
externalData: externalData,
|
||||
builder: (context, query) {
|
||||
if (query.isLoading || query.isRefetching || !query.hasData) {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
return Row(
|
||||
children: [
|
||||
Container(
|
||||
width: query.externalData,
|
||||
height: query.externalData,
|
||||
decoration: const BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: Colors.blue,
|
||||
),
|
||||
child: Center(child: Text(query.externalData.toString())),
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text("New Id"),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
externalData = Random().nextDouble() * 200;
|
||||
});
|
||||
},
|
||||
)
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.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 QueryPreviousDataExample extends StatefulWidget {
|
||||
const QueryPreviousDataExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<QueryPreviousDataExample> createState() =>
|
||||
_QueryPreviousDataExampleState();
|
||||
}
|
||||
|
||||
class _QueryPreviousDataExampleState extends State<QueryPreviousDataExample> {
|
||||
int id = 1;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Query Variable Key with keepPreviousData",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
QueryBuilder(
|
||||
job: todoJob(id.toString()),
|
||||
externalData: null,
|
||||
builder: (context, query) {
|
||||
if (query.hasError) return Text(query.error.toString());
|
||||
if (!query.hasData) return const CircularProgressIndicator();
|
||||
return Text(jsonEncode(query.data ?? {}));
|
||||
}),
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.remove),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
id -= 1;
|
||||
});
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
id += 1;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final queryVariableKeyJob = QueryJob.withVariableKey<String, void>(
|
||||
preQueryKey: "variable-query",
|
||||
task: (queryKey, externalData) {
|
||||
return Future.delayed(
|
||||
const Duration(milliseconds: 500),
|
||||
() => "QueryKey:${queryKey.split("#").last}",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
class QueryVariableKeyExample extends StatefulWidget {
|
||||
const QueryVariableKeyExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<QueryVariableKeyExample> createState() =>
|
||||
_QueryVariableKeyExampleState();
|
||||
}
|
||||
|
||||
class _QueryVariableKeyExampleState extends State<QueryVariableKeyExample> {
|
||||
late double id;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
id = Random().nextDouble() * 200;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"# Query Variable Example",
|
||||
style: Theme.of(context).textTheme.headline5,
|
||||
),
|
||||
QueryBuilder<String, void>(
|
||||
job: queryVariableKeyJob(id.toString()),
|
||||
externalData: null,
|
||||
builder: (context, query) {
|
||||
if (query.isLoading || query.isRefetching || !query.hasData) {
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
return Row(
|
||||
children: [
|
||||
Text("Query Result: ${query.data}"),
|
||||
ElevatedButton(
|
||||
child: const Text("New Id"),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
id = Random().nextDouble() * 200;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
//
|
||||
// Generated file. Do not edit.
|
||||
//
|
||||
|
||||
// ignore_for_file: directives_ordering
|
||||
// ignore_for_file: lines_longer_than_80_chars
|
||||
// ignore_for_file: depend_on_referenced_packages
|
||||
|
||||
import 'package:connectivity_plus_web/connectivity_plus_web.dart';
|
||||
|
||||
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
||||
|
||||
// ignore: public_member_api_docs
|
||||
void registerPlugins(Registrar registrar) {
|
||||
ConnectivityPlusPlugin.registerWith(registrar);
|
||||
registrar.registerMessageHandler();
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
import 'package:fl_query_example/components/basic_infinite_query.dart';
|
||||
import 'package:fl_query_example/components/basic_mutation.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query_example/components/basic_query.dart';
|
||||
import 'package:fl_query_example/components/infinite_query_disk_cache.dart';
|
||||
import 'package:fl_query_example/components/lazy_query.dart';
|
||||
import 'package:fl_query_example/components/mutation_variable_key.dart';
|
||||
import 'package:fl_query_example/components/query_disk_cache.dart';
|
||||
import 'package:fl_query_example/components/query_external_data.dart';
|
||||
import 'package:fl_query_example/components/query_previous_data.dart';
|
||||
import 'package:fl_query_example/components/query_variable_key.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await initializeFlQuery(cacheKey: "example");
|
||||
debugRepaintRainbowEnabled = true;
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return QueryBowlScope(
|
||||
bowl: QueryBowl(),
|
||||
child: MaterialApp(
|
||||
// showPerformanceOverlay: true,
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const MyHomePage(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<MyHomePage> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text("Fl Query Example"),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
const BasicQueryExample(),
|
||||
const QueryExternalDataExample(),
|
||||
const LazyQueryExample(),
|
||||
const QueryVariableKeyExample(),
|
||||
const QueryPreviousDataExample(),
|
||||
ListTile(
|
||||
title: const Text("Infinite Query Example"),
|
||||
trailing: const Icon(Icons.open_in_new),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const BasicInfiniteQueryExample(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const QueryDiskCacheExample(),
|
||||
const InfiniteQueryDiskCacheExample(),
|
||||
const Divider(),
|
||||
const BasicMutationExample(),
|
||||
const MutationVariableKeyExample(),
|
||||
],
|
||||
),
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user