Apreciaria mucho su ayuda. Estoy codificando mi primera aplicación en flutter. Hasta ahora estoy llamando a un script PHP en un servidor, que lee una base de datos MySql, este programa me devuelve un json que recibo perfectamente. Sin embargo, necesito mostrar en pantalla una lista con la información recolectada de la base de datos. Esta info es meteorológica y debo mostrarla en pantalla del móvil. Estoy completamente perdido, agradecería mucho cualquier ayuda. A continuación indico mi código.
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Lectura de estaciones',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Lectura de estaciones')
),
body: new ListView(
children: [
_buildItem('Primera Lectura'),
_buildItem('Segunda Lectura'),
_buildItem('Tercera Lectura'),
_buildItem('Cuarta Lectura')
]
)
)
);
}
}
Widget _buildItem(String textTitle) {
return new ListTile(
title: new Text(textTitle),
subtitle: new Text('Second station'),
onTap: (){
print(textTitle);
},
);
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
centerTitle: true,
),
);
}
Future getData() async{
var url = "http://10.0.2.2/appr3/selectapp.php";
http.Response response = await http.get(url);
if (response.statusCode == 200) {
var jsonResponse = jsonDecode(response.body);
print(jsonResponse.toString());
} else {
print('Request falla con status: ${response.statusCode}.');
}
}
@override
void initState() {
getData();
}
}
No se si voy bien o terriblemente mal, nunca he estado tan perdido. Necesito probar urgentemente que si se puede. Agradezco su ayuda
var jsonResponse
puedes utilizar unforeach
para que vayas leyendo todo el jsonkey=>value