Add workaround for pagination
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
kolaente 2020-04-27 18:39:28 +02:00
parent a34daacf23
commit 9fd47cde57
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 14 additions and 9 deletions

View File

@ -8,6 +8,7 @@ class Client {
final String _token;
final String _base;
int _maxPages;
int get maxPages => _maxPages;
String get base => _base;
@ -38,8 +39,8 @@ class Client {
port: uri.port,
path: uri.path,
query: uri.query,
queryParameters:
queryParameters, // Because dart takes a Map<String, String> here, it is only possible to sort by one parameter while the api supports n parameters.
queryParameters: queryParameters,
// Because dart takes a Map<String, String> here, it is only possible to sort by one parameter while the api supports n parameters.
fragment: uri.fragment);
return http.get(newUri, headers: _headers).then(_handleResponse);
}
@ -71,9 +72,16 @@ class Client {
throw new ApiException(
response.statusCode, response.request.url.toString());
}
_maxPages = response.headers["x-pagination-total-pages"] != null
? int.parse(response.headers["x-pagination-total-pages"])
: 0;
// FIXME: This is a workaround for when the client makes another
// unrelated api request in between requesting multiple pages of the same thing.
// To properly fix this, we need a way to pass the max number of pages somewhere else
// and not save them in the global client which everyone uses.
// This workaround only works when the other api requests in between two pages
// are not using pagination.
if (response.headers["x-pagination-total-pages"] != null) {
_maxPages = int.parse(response.headers["x-pagination-total-pages"]);
}
return _decoder.convert(response.body);
}
}
@ -81,6 +89,7 @@ class Client {
class ApiException implements Exception {
final int errorCode;
final String path;
ApiException(this.errorCode, this.path);
@override

View File

@ -73,10 +73,6 @@ class _ListPageState extends State<ListPage> {
.taskService
.maxPages) {
_currentPage++;
// FIXME: This does not get loaded until some time after the next (_tasks.length + 1) entry should have been rendered
// This leads to errors because the render method tries to access an entry of _tasks with an index which does not exist
// The load function gets actually called after the return below. I assume this is because of the setState() invocation
// in _loadTasksForPage()?
_loadTasksForPage(_currentPage);
}
return index < _tasks.length