I am trying to parse data from a Rest API inside a Dart/Flutter application. The JSON contains a field called budgets at the root, which contains a list of Words. I want to get a List from this JSON and showing table from the Json with the package json_table.
class _PresupuestoPageState extends State<PresupuestoPage> {
List <Map<String, dynamic>> _data;
@override
void initState() {
super.initState();
fetchData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Presupuesto"),
centerTitle: false
),
body: Container(
alignment: Alignment.center,
child: _data != null
? JsonTable(
_data,
)
: Text("Loading..."),
),
);
}
Future<void> fetchData() async {
try {
final response =
await http.get("http://pr_laravel.loc/api/budgets");
if (response.statusCode == 200) {
print(response.body);
setState(() {
_data = jsonDecode(response.body) as List;
});
} else {
print("Some error: ${response.statusCode}");
}
} catch (e) {
print(e);
}
}
}