onLazyLoad method
Future<PlutoInfinityScrollRowsResponse>
onLazyLoad(
- PlutoInfinityScrollRowsRequest request
)
Implementation
Future<PlutoInfinityScrollRowsResponse> onLazyLoad(
PlutoInfinityScrollRowsRequest request) async {
// rows are filtered?
var filter = request.filterRows.isNotEmpty;
// rows are sorted?
var sort = request.sortColumn != null && !request.sortColumn!.sort.isNone;
// number of records to fetch
var fetchSize = 50;
// index of last row fetched
var rowIdx = 0;
if (request.lastRow != null) {
var row = request.lastRow!;
rowIdx = rows.contains(row) ? rows.indexOf(row) : 0;
}
// build rows
if (filter || sort) {
// build all
buildAllRows();
} else {
buildOutRows(rowIdx + fetchSize);
}
// build the list
List<PlutoRow> tempList = rows.toList();
// filter the list
if (filter) {
tempList = applyFilters(tempList);
}
// sort the list
if (sort) {
tempList = applySort(tempList);
}
// Data needs to be implemented so that the next row
// to be fetched by the user is fetched from the server according to the value of lastRow.
//
// If [request.lastRow] is null, it corresponds to the first page.
// After that, implement request.lastRow to get the next row from the server.
//
// How many are fetched is not a concern in PlutoGrid.
// The user just needs to bring as many as they can get at one time.
//
// To convert data from server to PlutoRow
// You can convert it using [PlutoRow.fromJson].
// In the example, PlutoRow is already created, so it is not created separately.
Iterable<PlutoRow> fetchedRows = tempList.skipWhile(
(row) => request.lastRow != null && row.key != request.lastRow!.key);
if (request.lastRow == null) {
fetchedRows = fetchedRows.take(fetchSize);
} else {
fetchedRows = fetchedRows.skip(1).take(fetchSize);
}
// await Future.delayed(const Duration(milliseconds: 500));
// The return value returns the PlutoInfinityScrollRowsResponse class.
// isLast should be true when there is no more data to load.
// rows should pass a List<PlutoRow>.
// total number of rows
final bool isLast =
fetchedRows.isEmpty || widget.model.getDataRowCount() < rows.length;
// notify the user
if (isLast && mounted) {
//ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Last Page!')));
}
// convert to list
var list = fetchedRows.toList();
return Future.value(
PlutoInfinityScrollRowsResponse(isLast: isLast, rows: list));
}