Last active
May 13, 2020 10:29
-
-
Save tomasbaran/80e02a5a98028cb7cf7a206aa03157f7 to your computer and use it in GitHub Desktop.
[Flutter template] Fetch json 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('FetchListOfJsonsApi(): response.status: ${response.statusCode}'); | |
print('FetchListOfJsonsApi(): response.body: ${response.body}'); | |
print( | |
'FetchListOfJsonsApi(): response.runtimeType: ${response.runtimeType}'); | |
print( | |
'FetchListOfJsonsApi(): 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 in FetchListOfJsonsApi(): $e \n \n'); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment