我试图从服务器获取json数据
代码如下:void main() async{
List data = await getData();
print(data);
runApp(MyApp());
}
Future<List> getData() async {
String myUrl = "https://dashboard.ssitanas.com/public/api/categories";
http.Response response = await http.get(myUrl, headers: {
'Accept': 'application/json',
});
return json.decode(response.body);
}
有什么问题?
来自api的响应是Map,而不是List,但从外观上看,似乎在Map
中有一个列表那么就这样做:
var res = json.decode(response.body);
var listData = res["data"];
//assuming the list inside the map is called data
return listData;