close

[Solved] The argument type ‘Future’ can’t be assigned to the parameter type ‘void Function()’

Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. The argument type ‘Future’ can’t be assigned to the parameter type ‘void Function()’ in a flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How The argument type ‘Future’ can’t be assigned to the parameter type ‘void Function()’ Error Occurs ?

I am passing Data on Press of my flat button. When anyone press button they will redirect to another page with data. But it shows an error: The argument type ‘Future’ can’t be assigned to the parameter type ‘void Function()’.

Here is My code

        child: FlatButton(
          onPressed: 
          context, MaterialPageRoute(builder: (context)=>ProductDetailPage({productDetail:productDetail})))
        ),

How to Solve The argument type ‘Future’ can’t be assigned to the parameter type ‘void Function()’ Error?

  1. How to Solve The argument type 'Future' can't be assigned to the parameter type 'void Function()' Error?

    To Solve The argument type 'Future' can't be assigned to the parameter type 'void Function()' Error you need to use onPressed as function so that replace below the line. just use onPressed: () => Navigator.push(…) instead of onPressed: Navigator.push(…) will solve your error.

  2. The argument type 'Future' can't be assigned to the parameter type 'void Function()'

    To Solve The argument type 'Future' can't be assigned to the parameter type 'void Function()' Error you need to use onPressed as function so that replace below the line. just use onPressed: () => Navigator.push(…) instead of onPressed: Navigator.push(…) will solve your error.

Solution 1

First of all You need to use onPressed as function so that replace below line.

Replace

onPressed: Navigator.push(...)

with

onPressed: () => Navigator.push(...)

Now Your error might be solved. You can also use async/await here.

onPressed: () async {
  await Navigator.push(...);
}

Solution 2

This is worked for me in latest versions

 onPressed: () {
          selectHandler();
 },

Summery

So, It’s All About this Error. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.

Also Check Out Below Tutorials

Leave a Comment

%d bloggers like this: