简体   繁体   中英

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()'."

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() .

onRefresh expects a callback (a function) which would then be executed upon a refresh event. Here dart is telling you that you are providing the wrong argument type - a Future (the result of calling updateData ) instead of a function.

If you want to use async and await then,

Replace,

onRefresh: updateData(),

To,

 onRefresh: () async {
   await updateData();
    },
onRefresh: () async {
          updateData();
          return Future.delayed(const Duration(seconds: 1));
        }

void updateData() async {
  setState(() {
    dataFuture = getData();
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM