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
@@ -18,7 +18,6 @@ class QueryBuilder<DataType, ErrorType, KeyType> extends StatefulWidget {
final QueryFn<DataType> queryFn;
final ValueKey<KeyType> queryKey;
final QueryBuilderFn builder;
final DataType? initial;
final RetryConfig retryConfig;
@@ -30,6 +29,7 @@ class QueryBuilder<DataType, ErrorType, KeyType> extends StatefulWidget {
// widget specific
final bool enabled;
final QueryBuilderFn<DataType, ErrorType, KeyType> builder;
const QueryBuilder(
this.queryKey,
@@ -44,7 +44,7 @@ class QueryBuilder<DataType, ErrorType, KeyType> extends StatefulWidget {
this.enabled = true,
super.key,
}) : assert(
enabled && jsonConfig != null,
(jsonConfig != null && enabled) || jsonConfig == null,
'jsonConfig is only supported when enabled is true',
);
@@ -63,7 +63,7 @@ class _QueryBuilderState<DataType, ErrorType, KeyType>
StreamSubscription<ErrorType>? errorSubscription;
void update(_) {
setState(() {});
if (mounted) setState(() {});
}
Future<void> initialize() async {
@@ -115,6 +115,8 @@ class _QueryBuilderState<DataType, ErrorType, KeyType>
removeListener?.call();
initialize();
return;
} else if (oldWidget.enabled != widget.enabled && widget.enabled) {
query!.fetch();
}
if (oldWidget.queryFn != widget.queryFn) {
query!.updateQueryFn(widget.queryFn);
@@ -147,7 +149,8 @@ class _QueryBuilderState<DataType, ErrorType, KeyType>
DiagnosticsProperty<ValueKey<KeyType>>('queryKey', widget.queryKey),
);
properties.add(
DiagnosticsProperty<QueryBuilderFn>('builder', widget.builder),
DiagnosticsProperty<QueryBuilderFn<DataType, ErrorType, KeyType>>(
'builder', widget.builder),
);
properties.add(DiagnosticsProperty<DataType>('initial', widget.initial));
properties.add(