Created
April 29, 2020 07:52
-
-
Save sharmadhiraj/108dbd1a5ca145c3c91f0982c7e0ba6c to your computer and use it in GitHub Desktop.
This file contains 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> 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