gpt4 book ai didi

android-studio - 无法将参数类型 'Future'分配给参数类型 'int'

转载 作者:行者123 更新时间:2023-12-03 04:40:06 25 4
gpt4 key购买 nike

我是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;
}
}
希望对此有所帮助。谢谢

最佳答案

当您想显示将来函数的结果时,您需要使用FutureBuilder

FutureBuilder<int>(
future: _getTotalCalls(),
initalData: 0,
builder: (context, snapshot) {
return UpcomingCard(
title: 'Total Service Calls',
value: snapshot.data.toString(),
color: Colors.blue)
}
)

关于android-studio - 无法将参数类型 'Future<int>'分配给参数类型 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63393985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com