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.
Table of Contents
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?
- 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.
- 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
- How to make a blur Background Image effect in Flutter using BackdropFilter.
- Create Rounded Corners Image in Flutter.
- set Background Image to Scaffold in Flutter.
- How To Select multiple images with Flutter
- How to Set Network Image In Circular Avatar In Flutter?
Both the solutions worked for me, it saved my day. Thank you.