我可以从函数中获取返回值
我有一个名为totallabel()的类,该类从另一个函数中获得双重值:
Future<double> getValue(String type) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
double total = prefs.getDouble(type) ?? 0;
return total;
}
这就是我调用该函数以获取结果的方式:
TotalLabel(
typeOf: 'Hidrats de carboni',
subtitle: 'Range',
onPressed: () {},
fillBar:
getValue('hidratsDeCarboni') //gets result from function
)
问题是,当我调用函数传递函数时,传递了我想要获得的变量的ID时,我有一个错误:“参数类型'未来不能分配给参数类型double”。
谢谢。
I have a class called TotalLabel() that gets a double value from another function:
Future<double> getValue(String type) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
double total = prefs.getDouble(type) ?? 0;
return total;
}
This is how I call the function to get the result:
TotalLabel(
typeOf: 'Hidrats de carboni',
subtitle: 'Range',
onPressed: () {},
fillBar:
getValue('hidratsDeCarboni') //gets result from function
)
The problem is that when I call the function passing the ID of the variable that I want to get, I have this error: "The argument type 'Future can't be assigned to the parameter type double".
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getValue
返回future&lt; double&gt;
,因此您要么在initstate
中等待它,要么使用Future> FutureBuilder
getValue
returns aFuture<double>
so you either await for it in theinitState
or use aFutureBuilder
您正在从未来函数中返回值,您需要使用等待响应,然后使用该值。
you are returning the value from a future function you need to use await to wait for a response then use that value.