Last active
May 13, 2020 10:29
-
-
Save tomasbaran/ab28df02a20258a4fc6a82e8f0189455 to your computer and use it in GitHub Desktop.
[Flutter template] Fetch list of jsons api into your Flutter project.
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
// Call the method e.g. from initState like this: FetchListOfJsonsApi().getDecodedBody(); | |
// | |
// Install the http plugin here: https://pub.dev/packages/http | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
class FetchListOfJsonsApi { | |
final String url; | |
FetchListOfJsonsApi({ | |
this.url = 'https://www.googleapis.com/books/v1/volumes?q={http}', | |
}); | |
Future<List<dynamic>> getDecodedBody() async { | |
try { | |
http.Response response = await http.get(url); | |
int status = response.statusCode; | |
String body = response.body; | |
//print('response.status: ${response.statusCode}'); | |
//print('response.body: ${response.body}'); | |
//print('response.runtimeType: ${response.runtimeType}'); | |
//print('response.isRedirect: ${response.isRedirect}'); | |
if (status == 200 || status == 201) { | |
List<dynamic> decodedBody = jsonDecode(body); | |
//print('decodedBody: $decodedBody'); | |
return decodedBody; | |
} else | |
throw ('Status: $status'); | |
} catch (e) { | |
print('Error getting response: $e \n \n'); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment