feat: resolve and resolveWith helper support

This commit is contained in:
Kingkor Roy Tirtho
2023-06-23 22:40:20 +06:00
parent 8c7b19c0e5
commit a40dca73b5
12 changed files with 347 additions and 42 deletions
+8 -1
View File
@@ -28,7 +28,14 @@ class MainApp extends StatelessWidget {
),
title: 'FL Query Example',
builder: (context, child) {
return FlQueryDevtools(child: child);
return FlQueryDevtools(
child: QueryStateResolverProvider(
child: child!,
offline: () => const Center(
child: Text("Why u offline? How can u live?"),
),
),
);
},
routerConfig: router,
);
@@ -67,33 +67,40 @@ class _InfiniteQueryPageWidgetState extends State<InfiniteQueryPageWidget> {
return lastPage + 1;
},
initialPage: 0,
jsonConfig: JsonConfig(
fromJson: (json) => PagedProducts.fromJson(json),
toJson: (data) => data.toJson(),
),
// jsonConfig: JsonConfig(
// fromJson: (json) => PagedProducts.fromJson(json),
// toJson: (data) => data.toJson(),
// ),
builder: (context, query) {
final products = query.pages.map((e) => e.products).expand((e) => e);
if (!query.hasPages) {
return const Center(
return query.resolve(
(data) => ListView(
controller: controller,
children: [
for (final product in products)
ListTile(
title: Text(product.title),
subtitle: Text(product.description),
leading: Image.network(product.thumbnail),
),
if (query.hasNextPage)
const Center(
child: CircularProgressIndicator(),
),
if (query.hasErrors)
...query.errors.map((e) => Text(e.message)).toList(),
],
),
loading: () => const Center(
child: CircularProgressIndicator(),
);
}
return ListView(
controller: controller,
children: [
for (final product in products)
ListTile(
title: Text(product.title),
subtitle: Text(product.description),
leading: Image.network(product.thumbnail),
),
if (query.hasNextPage)
const Center(
child: CircularProgressIndicator(),
),
if (query.hasErrors)
...query.errors.map((e) => Text(e.message)).toList(),
],
),
error: (error) => const Center(
child: Text("Sorry! There was an error :'("),
),
offline: () => const Center(
child: Text("Yo. You're offline. Are you in a jungle?"),
),
);
},
),
+5 -11
View File
@@ -43,17 +43,11 @@ class QueryPage extends StatelessWidget {
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"),
return query.resolveWith(
context,
(data) => Center(child: Text(data)),
error: (error) => Center(child: Text(error.toString())),
loading: () => const Center(child: CircularProgressIndicator()),
);
},
),