Skip to content

Instantly share code, notes, and snippets.

@ulisseshen
Created June 26, 2022 18:12
Show Gist options
  • Save ulisseshen/7320517b7ab080a3f64188908f2c27f2 to your computer and use it in GitHub Desktop.
Save ulisseshen/7320517b7ab080a3f64188908f2c27f2 to your computer and use it in GitHub Desktop.
Future<Album> fetchAlbum() async {
final response = await http
.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'));
if (response.statusCode == 200) {
// Se o servidor retornou uma resposta 200 OK,
// então analisa o JSON.
return Album.fromJson(jsonDecode(response.body));
} else {
// Se o servidor não retornou uma resposta 200 OK,
// então lança uma exceção.
throw Exception('Failed to load album');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment