resolveSelectOffset function
Resolves the effective offset of a select* operation from a 1-based
page, where limit is the page size.
Returns offset unchanged when page is null, otherwise
(page - 1) * limit. A page of 1 resolves to an offset of 0, which
still activates the ordering (see OrderDirection.resolveOrderByID), so a
paginated select is stable by default.
Throws an ArgumentError when:
pageandoffsetare both defined: they are two spellings of the same thing, so passing both is a bug rather than a precedence question;pageis defined without a positivelimit: a page has no meaning without a page size, and alimitof0means "no limit";pageis< 1: pages are numbered from1.
Implementation
int? resolveSelectOffset({int? page, int? offset, int? limit}) {
if (page == null) return offset;
if (offset != null) {
throw ArgumentError.value(
page,
'page',
"`page` and `offset` are mutually exclusive (offset: $offset)",
);
}
if (limit == null || limit <= 0) {
throw ArgumentError.value(
page,
'page',
'`page` requires a positive `limit` (the page size), got: $limit',
);
}
if (page < 1) {
throw ArgumentError.value(page, 'page', '`page` is 1-based, must be >= 1');
}
return (page - 1) * limit;
}