我是Flutter的新手,我一直在尝试开发具有PHP API后端的应用程序。在无数的答案和在线研究中,我无法找到解决所面临问题的方法,希望有人可以就我做错的事情给我提供意见。
我正在尝试从API中获取数据以显示在卡中。下面是我的代码:
...previous unnecessary code
Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0, vertical: 30.0),
child: Text('Summary Report',
style: TextStyle(
color: Colors.black.withOpacity(0.7),
fontWeight: FontWeight.bold,
fontSize: 20.0))),
Padding(
padding: EdgeInsets.only(left: 15.0, bottom: 25.0, right: 15.0),
child: Container(
height: 150.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
UpcomingCard(
title: 'Total Service Calls',
value: _getTotalCalls(), //this is where I want to display my data
color: Colors.blue),
UpcomingCard(
title: 'Pending Service Calls',
value: 0,
color: Colors.red),
],
),
),
... other unnecessary code
//function to get data
Future<int> _getTotalCalls() async {
SharedPreferences localStorage = await SharedPreferences.getInstance();
var user_id = localStorage.getString('user_id');
var data = {'user_id': user_id};
var res = await CallApi().postData(data, 'service/total');
var body = json.decode(res.body);
if (body['success']) {
return body['servicecall_total'];
} else {
return 0;
}
}
希望对此有所帮助。谢谢
答案 0 :(得分:1)
要显示将来函数的结果时,需要使用FutureBuilder
FutureBuilder<int>(
future: _getTotalCalls(),
initalData: 0,
builder: (context, snapshot) {
return UpcomingCard(
title: 'Total Service Calls',
value: snapshot.data.toString(),
color: Colors.blue)
}
)