Last active
October 2, 2021 07:59
-
-
Save naimurhasan/bccf229d8b52bafbeb3a7990150e1cda to your computer and use it in GitHub Desktop.
dart to get fetch pagination content from django backend with 'next'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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