Created
October 11, 2019 02:53
-
-
Save israel-dv/a9a67fd6e2e00d06a4e9bfe89095d6c3 to your computer and use it in GitHub Desktop.
Clase HttpHandle para mostrar contenido de API JSONPlaceHolder - Future Async Await - Medium
This file contains hidden or 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
import 'dart:convert'; | |
import 'package:http/http.dart' as http; | |
class HttpHandle{ | |
//Funcion que valida si la peticion se hizo correctamente | |
Future<Map> isConnect(String id) async { | |
//La primera validación la hacemos en caso de no ingresar un ID | |
if(id == '') | |
return null; | |
try{ | |
http.Response response = await http.get('https://jsonplaceholder.typicode.com/posts/$id'); | |
//si es codigo http es igual a 200 la petición fue exitosa y retornamos el JSON, en este caso un MAP | |
if(response.statusCode == 200) | |
return jsonDecode(response.body); | |
else | |
return null; | |
} catch (e) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment