feat: resolve and resolveWith helper support
This commit is contained in:
@@ -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?"),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -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()),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user