Michael S
Michael S

Reputation: 769

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

Upvotes: 0

Views: 2484

Answers (3)

Jaydeep chatrola
Jaydeep chatrola

Reputation: 2701

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

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

Upvotes: 0

Yashraj
Yashraj

Reputation: 999

If you want to use async and await then,

Replace,

onRefresh: updateData(),

To,

 onRefresh: () async {
   await updateData();
    },

Upvotes: 1

Tomer Ariel
Tomer Ariel

Reputation: 1537

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.

Upvotes: 3

Related Questions