PagyController<T> class
A controller that manages paginated API data and local item modifications.
PagyController is responsible for:
- Fetching data from an API endpoint with pagination
- Managing internal state via PagyState
- Retrying failed requests with cached parameters
- Allowing local updates to the item list without re-fetching
Key Responsibilities:
- Holds the list of items and exposes them as read-only
- Stores pagination, filter, and query parameters
- Handles cancellation of requests
- Integrates with
PagyBuilder,PagyListView, andPagyGridView
Example Usage:
final controller = PagyController<User>(
endPoint: '/users',
fromMap: (json) => User.fromJson(json),
responseMapper: (response) => PagyResponseParser.fromJson(response),
limit: 20,
);
// Trigger load
controller.loadData();
- Available extensions
Constructors
-
PagyController({required String endPoint, required T fromMap(Map<
String, dynamic> ), @Deprecated('Use responseParser instead') PagyResponseParser responseMapper(Map<String, dynamic> response)?, PagyResponseParser responseParser(Map<String, dynamic> response)?, String? token, @Deprecated('Use query instead') Map<String, dynamic> ? additionalQueryParams, Map<String, dynamic> ? query, int limit = 4, @Deprecated('Use payloadMode instead') PaginationPayloadMode? paginationMode, PaginationPayloadMode? payloadMode, dynamic payloadData, dynamic headers, PagyApiRequestType requestType = PagyApiRequestType.get}) - Creates a PagyController for managing paginated data from an API.
Properties
-
additionalQueryParams
→ Map<
String, dynamic> ? -
Extra query parameters appended to pagination params.
final
- cancelToken ↔ CancelToken?
-
Internal cancel token to abort API requests in-flight.
getter/setter pair
-
controller
→ ValueNotifier<
PagyState< T> > -
Holds the API pagination state and notifies listeners on updates.
final
- endPoint → String
-
The API endpoint used for fetching paginated data.
final
-
filter
↔ Map<
String, dynamic> ? -
Internal filter object for persisting last applied filters.
getter/setter pair
-
fromMap
→ T Function(Map<
String, dynamic> ) -
Converts raw JSON response into a model object of type
T.final - hashCode → int
-
The hash code for this object.
no setterinherited
- headers → dynamic
-
Optional custom headers for API requests.
final
-
items
→ List<
T> -
Read-only, unmodifiable view of the current items list.
no setter
-
itemsList
→ List<
T> -
Internal storage of fetched items.
final
-
lastParams
↔ Map<
String, dynamic> ? -
Last used request parameters, stored for retry functionality.
getter/setter pair
- limit → int
-
Number of items to fetch per page (default:
4).final - metadata → PagyMetadata
-
Returns pagination metadata for UI display.
no setter
- paginationMode → PaginationPayloadMode?
-
Determines whether pagination params are sent as query or payload.
final
- payloadData → dynamic
-
Optional static payload data included in API requests.
final
- payloadMode → PaginationPayloadMode?
-
Mode for sending pagination parameters (query string or request body).
final
-
query
→ Map<
String, dynamic> ? -
Query parameters to append to API requests.
final
- requestType → PagyApiRequestType
-
Defines the HTTP method used for requests.
final
-
responseMapper
→ PagyResponseParser Function(Map<
String, dynamic> response)? -
Function to parse API response into a PagyResponseParser.
final
-
responseParser
→ PagyResponseParser Function(Map<
String, dynamic> response)? -
Function to parse API response into a PagyResponseParser.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
state
→ PagyState<
T> -
Current pagination state exposed from controller.
no setter
- token → String?
-
Optional token to include in API request headers (e.g., Bearer token).
final
Methods
-
addItem(
T item, {bool atStart = false}) → void -
Available on PagyController<
Adds a singleT> , provided by the PagyControllerHelpers extensionitemto the list. -
addItems(
List< T> items, {bool atStart = false}) → void -
Available on PagyController<
Adds multipleT> , provided by the PagyControllerHelpers extensionitemsto the list. -
applyFilters(
Map< String, dynamic> filters) → Future<void> - Applies filters and reloads data from page 1.
-
dispose(
) → void -
Available on PagyController<
Disposes the underlying ValueNotifier to free resources.T> , provided by the PagyControllerHelpers extension -
insertAt(
int index, T item) → void -
Available on PagyController<
Inserts anT> , provided by the PagyControllerHelpers extensionitemat a specificindex. -
listen(
void onChanged(List< T> items)) → void -
Available on PagyController<
Attaches a listener that is called whenever the items list changes.T> , provided by the PagyControllerHelpers extension -
loadData(
{bool refresh = true, Map< String, dynamic> ? queryParameter, PaginationPayloadMode? paginationMode, dynamic payloadData}) → Future<void> -
Available on PagyController<
Loads paginated data from the API.T> , provided by the PagyControllerLoader extension -
loadMore(
) → Future< void> - Explicitly loads the next page of data.
-
mapItems(
T mapper(T old)) → void -
Available on PagyController<
Maps all items in the list using the providedT> , provided by the PagyControllerHelpers extensionmapperfunction. -
modifyDirect(
ValueUpdater< PagyState< updater) → voidT> > -
Available on PagyController<
Directly modifies the PagyState using anT> , provided by the PagyControllerHelpers extensionupdatercallback. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refresh(
) → Future< void> - Clears current data and reloads from page 1.
-
removeWhere(
bool test(T item)) → void -
Available on PagyController<
Removes items that match the givenT> , provided by the PagyControllerHelpers extensiontestcondition. -
replaceWhere(
bool test(T item), T newItem) → void -
Available on PagyController<
Replaces the first item matchingT> , provided by the PagyControllerHelpers extensiontestwithnewItem. -
reset(
) → void -
Available on PagyController<
Resets the controller to its initial empty state.T> , provided by the PagyControllerHelpers extension -
retry(
) → Future< void> -
Available on PagyController<
Retries the last failed request if parameters exist.T> , provided by the PagyControllerLoader extension -
search(
String query, {String searchKey = 'q'}) → Future< void> - Performs a search with the given query.
-
toString(
) → String -
A string representation of this object.
inherited
-
updateData(
List< T> newData) → void -
Available on PagyController<
Replaces the entire dataset withT> , provided by the PagyControllerHelpers extensionnewData. -
updateItemAt(
int index, T newItem) → void -
Available on PagyController<
Updates the item atT> , provided by the PagyControllerHelpers extensionindexwithnewItem.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited