feat: add infinite query builder with example

fix(query): updateQueryFn refetch when stale and update state of builders when widget is mounted
This commit is contained in:
Kingkor Roy Tirtho
2023-02-21 12:03:45 +06:00
parent 975f9eafe1
commit efa2c81505
21 changed files with 778 additions and 143 deletions
@@ -0,0 +1,62 @@
import 'dart:math';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/material.dart';
class QueryPage extends StatelessWidget {
const QueryPage({super.key});
@override
Widget build(BuildContext context) {
final value = Random().nextInt(200000);
return Scaffold(
appBar: AppBar(
title: const Text('Query'),
),
floatingActionButton: QueryListenable<String, dynamic, String>(
const ValueKey('hello'), builder: (context, query) {
if (query == null) {
return const SizedBox();
}
return FloatingActionButton(
onPressed: () {
query.refresh();
},
child: Text(query.data ?? 'No Data'),
);
}),
body: QueryBuilder<String, dynamic, String>(
const ValueKey('hello'),
() {
return Future.delayed(
const Duration(seconds: 6), () => 'Hello World! $value');
},
initial: 'Hello',
jsonConfig: JsonConfig(
fromJson: (json) => json['data'],
toJson: (data) => {'data': data},
),
onData: (value) {
debugPrint('onData: $value');
},
onError: (error) {
debugPrint('onError: $error');
},
builder: (context, query) {
if (query.isLoading) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (query.hasError) {
return Center(
child: Text(query.error.toString()),
);
}
return Center(
child: Text(query.data ?? "Unfortunately, there's no data"),
);
},
),
);
}
}