@@ -52,14 +52,18 @@ Future<void> updateProducts({
5252 notifyListeners();
5353 }
5454
55- final res = await apiFunction(
56- // startKey is optional and only required when you have pagination support in api
57- startKey: reset ? null : _productsResponse?.paginationKey,
58- );
59- if (reset || _productsResponse == null) {
60- _productsResponse = res;
61- } else {
62- _productsResponse!.update(res);
55+ try {
56+ final res = await apiFunction(
57+ // startKey is optional and only required when you have pagination support in api
58+ startKey: reset ? null : _productsResponse?.paginationKey,
59+ );
60+ if (reset || _productsResponse == null) {
61+ _productsResponse = res;
62+ } else {
63+ _productsResponse!.update(res);
64+ }
65+ } catch(_) {
66+ // handle error
6367 }
6468 notifyListeners();
6569}
@@ -72,23 +76,36 @@ Future<PaginatedItemsResponse<Product>?> apiFunction({
7276 // can be string or int (page number) or any other type.
7377 dynamic startKey,
7478}) async {
75- // startKey necessary if pagination support
76- final res = await _api.getProducts(startKey: startKey);
77-
78- return PaginatedItemsResponse<Product>(
79-
80- // list of items
81- listItems: res.data?.products,
82-
83- // only required to pass if pagination supported, else null. (can be of any type)
84- paginationKey: res.data?.paginationKey,
85-
86- // unique id, should only be passed in the repository function.
87- // required for functions like `updateItem`, `findByUid`
88- // and avoiding duplication of items in list (compares uid)
89- idGetter: (product) => product.id,
90-
91- );
79+ try {
80+ // startKey necessary if pagination support
81+ final res = await _api.getProducts(startKey: startKey);
82+
83+ return PaginatedItemsResponse<Product>(
84+
85+ // list of items
86+ listItems: res.data?.products,
87+
88+ // only required to pass if pagination supported, else null. (can be of any type)
89+ paginationKey: res.data?.paginationKey,
90+
91+ // unique id, should only be passed in the repository function.
92+ // required for functions like `updateItem`, `findByUid`
93+ // and avoiding duplication of items in list (compares uid)
94+ idGetter: (product) => product.id,
95+
96+ );
97+ } catch(_) {
98+ // handling error is important as if null is returned, the `response` becomes null,
99+ // which in turn causes infinite loading...
100+
101+ // hence, returning a response with empty list, so that screen shows the no items found text,
102+ // so that the user can refresh the contents, and you can probably have a snackBar or toast,
103+ // notifying the user of the error...
104+ return PaginatedItemsResponse<Product>(
105+ listItems: [],
106+ idGetter: (product) => product.id,
107+ );
108+ }
92109}
93110```
94111
@@ -145,6 +162,7 @@ Future<PaginatedItemsResponse<Post>?> updatePosts(dynamic paginationKey) async {
145162
146163PaginationItemsStateHandler<Post>(
147164 fetchPageData: updatePosts,
165+ showLoaderOnResetBuilder: (itemsFetchScope) => itemsFetchScope == ItemsFetchScope.noItemsRefresh,
148166 builder: (response, fetchPageData) {
149167 return PaginatedItemsBuilder<Post>(
150168 response: response,
0 commit comments