Skip to content

Instantly share code, notes, and snippets.

@naimurhasan
Last active October 2, 2021 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naimurhasan/bccf229d8b52bafbeb3a7990150e1cda to your computer and use it in GitHub Desktop.
Save naimurhasan/bccf229d8b52bafbeb3a7990150e1cda to your computer and use it in GitHub Desktop.
dart to get fetch pagination content from django backend with 'next'
Future<void> updateIfHasUpdate() async {
final String dataFirstFetchUri = "https://sample.django.backend.api/"
final List updatableQuestions = await _getUpdatableQuestions(dataFirstFetchUri);
}
Future<List<dynamic>> _getUpdatableQuestions(nextUri) async{
// THIS FUNCTIONS LOOP THROUGH PAGINATION
// AND RETURN data results concat
List updatableQuestions = [];
while(nextUri != null){
print('fetching for $nextUri');
try{
var response = await http.get(Uri.parse(nextUri));
if (response.statusCode == 200) {
final data = json.decode(utf8.decode(response.bodyBytes));
updatableQuestions+= data['results'];
// update next page uri from api responded next page link
nextUri = data['next'];
}else{
// so the loop doesn't repeat
nextUri = null;
}
}catch(err, stacktrace){
print('Updatable Question Fetch Pagination Error');
print(err);
print(stacktrace);
error_toast('Network error.');
// so the loop doesn't repeat
nextUri = null;
}
}
return updatableQuestions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment