【发布时间】:2021-03-27 04:15:11
【问题描述】:
我正在尝试从 Dart/Flutter 应用程序中的 Rest API 解析数据。 JSON 在根中包含一个名为 budgets 的字段,其中包含单词列表。我想从这个 JSON 中获取一个列表,并从带有包 json_table 的 Json 中显示表。
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);
}
}
}
【问题讨论】: