简体   繁体   English

颤振 - 未来<void>不能赋值给参数类型Future<void> 功能()

[英]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,如果你想使用asyncawait

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.

相关问题 参数 Future 不能分配给参数类型 void Function() - The argument Future can not be assigned to the parameter type void Function() 参数类型 'Future<void> Function()' 不能分配给参数类型 'Future<bool> 功能()?</bool></void> - The argument type 'Future<void> Function()' can't be assigned to the parameter type 'Future<bool> Function()? 参数类型 &#39;Future<void> Function(FirebaseUser)&#39; 不能分配给参数类型 &#39;void Function(User)&#39; - The argument type 'Future<void> Function(FirebaseUser)' can't be assigned to the parameter type 'void Function(User)' 参数类型 'Future<void> Function(String?)' 不能分配给参数类型 'void Function(NotificationResponse)?</void> - The argument type 'Future<void> Function(String?)' can't be assigned to the parameter type 'void Function(NotificationResponse)? 参数类型&#39;Future<void> Function(TapPosition)&#39; 不能分配给参数类型&#39;void Function(TapPosition, LatLng)?&#39; - The argument type 'Future<void> Function(TapPosition)' can't be assigned to the parameter type 'void Function(TapPosition, LatLng)?' 参数类型 'Future<null> Function(String)' 不能分配给参数类型 'void Function(NotificationResponse)'</null> - The argument type 'Future<Null> Function(String)' can't be assigned to the parameter type 'void Function(NotificationResponse)' 参数类型&#39;Future<dynamic> 函数(字符串)”不能分配给参数类型“无效函数(字符串?)?” - The argument type 'Future<dynamic> Function(String)' can't be assigned to the parameter type 'void Function(String?)?' 错误:参数类型“Future”不能分配给参数类型“void Function()” - error: The argument type 'Future' can't be assigned to the parameter type 'void Function()' 错误:参数类型 'Future<dynamic> ' 不能分配给参数类型 'void Function()?'</dynamic> - Error: The argument type 'Future<dynamic>' can't be assigned to the parameter type 'void Function()?' 参数类型'Future <list<user> &gt;' 不能分配给参数类型'Future<void> 使用 RefreshIndicator 时 onRefresh 中的 Function()' </void></list<user> - The argument type 'Future<List<User>>' can't be assigned to the parameter type 'Future<void> Function()' in onRefresh when using RefreshIndicator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM