PaginatedApiCallBuilder<T extends Decodable, E extends Decodable> constructor

const PaginatedApiCallBuilder<T extends Decodable, E extends Decodable>({
  1. Key? key,
  2. required PaginatedAPIRequest<T, E> apiCall,
  3. required Widget builder(
    1. BuildContext context,
    2. List<T>? response
    ),
  4. Widget loaderBuilder(
    1. BuildContext context
    )?,
  5. Widget errorBuilder(
    1. BuildContext context,
    2. String? errorMessage
    )?,
  6. Map<String, String>? initialQuery,
  7. Widget emptyDataBuilder(
    1. BuildContext context
    )?,
  8. int initialPage = 0,
})

This widget helps to manage transparently a paginated API call in the widget tree showing a loading widget while performing the request and providing access to the response in the builder parameter, to show a proper widget on http call completion.

Furthermore, it can manage pagination while scrolling.

It must be used with the ConnectionManager, that is what manages API call and states through a bloc component.

T and E are, respectively, the class to decode in the success response and the class to decode for an error response. If not provided, the builder will have a generic Object as argument, that then should be casted to use.

 Future<PaginatedAPIResponse<User, Error>> doApiRequest(
    int page, Map<String, String>? query) async {
  var response = await context.read<NetworkProvider>().connectionManager.doApiRequest(
    requestType: ApiRequestType.get,
    endpoint: "/test-endpoint",
    decodeContentFromMap: User.fromMap,
  );
  if (response.hasError) {
    return PaginatedAPIResponse.error(response: response);
  }

  return PaginatedAPIResponse.success(response.decodedBody.data ?? [], // Note that `response.decodedBody.data` depends on your decoded model, this is an example
      response: response, page: page, pageSize: 25);
}

PaginatedApiCallBuilder<User, Error>(
  apiCall: doApiRequest,
  builder: (context, response) {
    return Text(response.toString());
  },
);

Implementation

const PaginatedApiCallBuilder({
  Key? key,
  required this.apiCall,
  required this.builder,
  this.loaderBuilder,
  this.errorBuilder,
  this.initialQuery,
  this.emptyDataBuilder,
  this.initialPage = 0,
}) : super(key: key);