我是flutter/ dart的新手,在一个web服务驱动的flutter应用程序中工作。我目前在映射和显示嵌套的JSON数据时遇到了一个问题。
我需要处理的JSON是由我本地的php服务器JSON格式提供的,我已经尝试并正在使用代码:
BD: "Bangladesh"
BE: "Belgium"
BF: "Burkina Faso"
BG: "Bulgaria"代码:
Map _countries = new Map();
void _getData() async {
var url = 'http://country.io/names.json';
var response = await http.post(url);
if (response.statusCode == 200) {
setState(() => _countries = json.decode(response.body));
debugPrint('Loaded ${_countries.length} data.');
}
}用于显示数据的颤动构件:
return Card(
child: new Row(
children: <Widget>[
Expanded(
child: new Text(
'${key} : ',
style: TextStyle(fontSize: 28.0),
),
),
Expanded(
child: new Text(_counties[key],
style: TextStyle(fontSize: 28.0)),
)
],
)
);我有服务器端php代码工作,并提供以下json数据的POST请求
JSON数据我希望映射和显示类似于上面的内容:
{
"Employee_id1": {
"first_name": "Staff",
"last_name": "EFGH",
"contact": "1223234555",
"designation": "des1",
"department": "d1",
"picture": "http://i.pravatar.cc/300"
},
"Employee_id2": {
"first_name": "Staff",
"last_name": "EFGH",
"contact": "1223234555",
"designation": "des1",
"department": "d2",
"picture": "http://i.pravatar.cc/300"
},
}连接发布请求代码:
void _getData() async {
var url = 'http://myIP/file.php';
var response = await http.post(url, body:
{"staffprofiles":"showStaffs"});
if (response.statusCode == 200) {
setState(() => _staffs = json.decode(response.body));
debugPrint('Loaded ${_staffs.length} staff profiles.');
}
}我希望将JSON显示为我从ListView生成器中的服务器获得的许多员工配置文件的配置文件卡(在卡中
发布于 2019-04-28 01:43:41
经过几次点击和试验,我发现我所需要的就是在dart中嵌套一个地图,
Map[key][subkey]下面的代码解决了我的问题:
return Card(
child: new Row(
children: <Widget>[
Expanded(
child: new Text(
'${key} : ',
style: TextStyle(fontSize: 28.0),
),
),
Expanded(
child: new Text(_employees[key]["first_name"],
style: TextStyle(fontSize: 28.0)),
)
],
)
);https://stackoverflow.com/questions/55853790
复制相似问题