PaginatedApiCallBuilder<T extends Decodable, E extends Decodable> constructor
const
PaginatedApiCallBuilder<T extends Decodable, E extends Decodable> ({
- Key? key,
- required PaginatedAPIRequest<
T, E> apiCall, - required Widget builder(
- BuildContext context,
- List<
T> ? response
- Widget loaderBuilder(
- BuildContext context
- Widget errorBuilder(
- BuildContext context,
- String? errorMessage
- Map<
String, String> ? initialQuery, - Widget emptyDataBuilder(
- BuildContext context
- 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);