I've got a problem with my code. I try to show on my App this function that calls a class that has an API request. When I try to print the data returned, it only prints Instance of 'Future<Map<dynamic,dynamic>>' instead of what it is expected (the Magnitud value).
I'm quite new to Flutter, so every tip is well-received and I would appreciate it.
My class is
class GaelApi {
String magnitud;
String ubicacion;
String latitud;
String longitud;
String hora;
Future<void> getDatos(int index) async {
try {
// obtiene datos
Response response =
await get("https://api.gael.cl/general/public/sismos");
List<dynamic> data = jsonDecode(response.body);
Map primerSismo = data[0];
// obtener las propiedades
magnitud = primerSismo['Magnitud'];
ubicacion = primerSismo['RefGeografica'];
latitud = primerSismo['Latitud'];
longitud = primerSismo['Longitud'];
hora = primerSismo['Fecha'];
} catch (e) {
print(e);
magnitud = 'No se pudo calcular la magnitud';
ubicacion = 'No se pudo calcular la ubicacion';
hora = 'No se pudo calcular la hora';
}
}
}
and what I tried is
class _SismosState extends State<Sismos> {
Future<Map> recabarDatos(int index) async {
GaelApi instancia = GaelApi();
await instancia.getDatos(index);
Map<String, String> datos = {'magnitud': instancia.magnitud};
print(datos);
return datos;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 5,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 1.0, horizontal: 5.0),
child: Card(
child: ListTile(
title: Text("${recabarDatos(index)}",
style: TextStyle(fontSize: 17))),
),
);
},
),
);
}
}
and a picture is this link
Thank you!