The argument type 'Function' can't be assigned to the parameter type 'void Function()?' after null safety
Solution 1
Change your code to accept a VoidCallback
instead of Function
for the onPressed
.
By the way VoidCallback
is just shorthand for void Function()
so you could also define it as final void Function() onPressed;
Updated code:
class DrawerItem extends StatelessWidget {
final String text;
final VoidCallback onPressed;
const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return FlatButton(
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18.0,
),
),
onPressed: onPressed,
);
}
}
Solution 2
Dart 2.12 (Null safety):
Instead of
final Function? onPressed; // Bad
use
final void Function()? onPressed; // Good
final VoidCallback? onPressed; // Good
Solution 3
Well that's because onPressed
inside FlatButton
is not a normal function its VoidCallBack
Function.
You can try something like this:
final VoidCallBack onPressed;
While, you are passing a normal function
into a VoidCallBack
Follow the official doc here
Updated Code:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
_myFunction() => print("Being pressed!");
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DrawerItem(
text: "Hello Jee",
onPressed: _myFunction,
),
],
),
),
);
}
}
class DrawerItem extends StatelessWidget {
final String text;
final Function onPressed;
const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return FlatButton(
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 18.0,
),
),
onPressed: onPressed,
);
}
}
Solution 4
Put the parentheses ()
after the keyword Function
when defining the function.
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final Function() onPressed; //parenthesis () on function
final String name;
const Answer(this.name, this.functionMessage, {Key? key}) : super(key:
key);
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.only(top: 10),
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.black, // background
onPrimary: Colors.white, // foreground
),
onPressed: onPressed,
child: Text(name),
));
}
}
Solution 5
use:
VoidCallback? _onPressed
instead of:
VoidCallback _onPressed
it's work for me!
dosytres
Updated on July 08, 2022Comments
-
dosytres almost 2 years
I want to achieve to make a drawer with different items on it, so I am creating a separate file for the
DrawerItems
and the with the constructor, pass the data to the main file. But I get the following error on theonPressed
function:"The argument type 'Function' can't be assigned to the parameter type 'void Function()'"
class DrawerItem extends StatelessWidget { final String text; final Function onPressed; const DrawerItem({Key key, this.text, this.onPressed}) : super(key: key); @override Widget build(BuildContext context) { return FlatButton( child: Text( text, style: TextStyle( fontWeight: FontWeight.w600, fontSize: 18.0, ), ), onPressed: onPressed, ); } }
Anyone knows why?
-
dosytres over 3 yearsNo, its not that the problem is still using final VoidCallBack onPressed; But thank you.
-
Hamza over 3 yearsYour code is working fine for me, what's your Flutter version? Or can you show me which
function
are you passing as parameter toDrawerItem
-
dosytres over 3 yearsmy flutter version is 1.22.o, I know its old, but I have to use this one because i have an error with java and it wont work with an updated one.
-
Hamza over 3 yearsActually your code is fine here, can you show me which type of
function
you are passing as a parameter? -
dosytres over 3 yearsThis is the full code, then iI want to be able to pass it to another file with the constructor
-
Askani about 3 yearsfinal Function()? onPressed; worked for me , thank you :)
-
Hitesh Danidhariya about 3 yearsIs there any solution if I want a function with argument in it.For example Textfield function "onSubmitted", because it gives me the error as "The argument type 'void Function()?' can't be assigned to the parameter type 'void Function(String)?".Any solution for this error?
-
Pieter van Loon about 3 years@HiteshDanidhariya yes you can simply add it in the brackets, i.e.
void Function(String myString)
-
vietstone almost 3 yearsCan you explain the logic behind it? Or point me some link about this? Thank you.
-
CopsOnRoad almost 3 years@vietstone A
Function
can be anything, likeFunction()
,Function(int)
, etc, which is why with Dart null safety, you should explicitly tell what thatFunction
is. -
vietstone almost 3 years@CopsOnRoad Thank you for your point. But I wonder that, as you say about Function() and Function(int), I think this is a kind of Polymorphism, not really Null Safety :D
-
CopsOnRoad almost 3 years@vietstone Polymorphism is a different thing.
Function onPressed
can be invoked byonPressed()
,onPressed(1)
,onPressed(1, true)
so, you may run into runtime error butFunction() onPressed
can only be invoked byonPressed()
. This thing is not particular related to null safety but Dart 2.12. -
vietstone almost 3 years@CopsOnRoad yes, just curious about the name of this technique specifically
-
sina about 2 yearsshort & effective answer... thanks :)