Skip to content

Instantly share code, notes, and snippets.

@sharmadhiraj
Created April 29, 2020 07:52
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 sharmadhiraj/108dbd1a5ca145c3c91f0982c7e0ba6c to your computer and use it in GitHub Desktop.
Save sharmadhiraj/108dbd1a5ca145c3c91f0982c7e0ba6c to your computer and use it in GitHub Desktop.
Future<void> fetchPhotos() async {
try {
final response = await http.get(
"https://jsonplaceholder.typicode.com/photos?_page=$_pageNumber");
List<Photo> fetchedPhotos = Photo.parseList(json.decode(response.body));
setState(() {
_hasMore = fetchedPhotos.length == _defaultPhotosPerPageCount;
_loading = false;
_pageNumber = _pageNumber + 1;
_photos.addAll(fetchedPhotos);
});
} catch (e) {
setState(() {
_loading = false;
_error = true;
});
}
}
}
class Photo {
final String title;
final String thumbnailUrl;
Photo(this.title, this.thumbnailUrl);
factory Photo.fromJson(Map<String, dynamic> json) {
return Photo(json["title"], json["thumbnailUrl"]);
}
static List<Photo> parseList(List<dynamic> list) {
return list.map((i) => Photo.fromJson(i)).toList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment