[英]Flutter - Future<void> can not be assigned to the parameter type Future<void> Function()
I guess I am blind, but I can not see the problem... Maybe someone can help me.我想我是盲人,但我看不到问题......也许有人可以帮助我。
The Problem is in this line "onRefresh: updateData()" and the full message is "The argument type 'Future' can't be assigned to the parameter type 'Future Function()'."问题出在“onRefresh: updateData()”这一行,完整的消息是“无法将参数类型‘Future’分配给参数类型‘Future Function()’。”
late Future<DocumentSnapshot> dataFuture;
Future<void> updateData() async {
setState(() {
dataFuture = getData();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: RefreshIndicator(
onRefresh: updateData(),
...
I think the problem is that you're calling the function rather than providing it as an argument - should be just updateData
rather than updateData()
.我认为问题在于您正在调用该函数而不是将其作为参数提供-应该只是
updateData
而不是updateData()
。
onRefresh
expects a callback (a function) which would then be executed upon a refresh event. onRefresh
需要一个回调(一个函数),然后在刷新事件时执行。 Here dart is telling you that you are providing the wrong argument type - a Future
(the result of calling updateData
) instead of a function.这里 dart 告诉您您提供了错误的参数类型 -
Future
(调用updateData
的结果)而不是函数。
If you want to use async
and await
then,如果你想使用
async
和await
,
Replace,代替,
onRefresh: updateData(),
To,至,
onRefresh: () async {
await updateData();
},
onRefresh: () async {
updateData();
return Future.delayed(const Duration(seconds: 1));
}
void updateData() async {
setState(() {
dataFuture = getData();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.