The argument type 'Function' can't be assigned to the parameter type 'void Function()? this error is shown in onpressed section - flutter-onpressed

import 'package:flutter/material.dart';
class Answer extends StatelessWidget{
final Function selectHandler;
Answer(this.selectHandler);
Widget build(BuildContext context){
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.green,
child: Text('Answer1'),
onPressed: selectHandler,
),
);
}
}

Related

How can I Remove an error While Defining return type as VoidCallback of a property?

I get an error Whenever I try to use VoidCallback instead of function when i Define GetAnswer.I am using it as a pointer to get value in it in the constructor below.But I am continuously getting an error shown in the 2nd pic.Seniors Plz Guide
import 'dart:html';
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback GetAnswer;
Answer(this.GetAnswer);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.blue,
child: Text("4 CGPA"),
onPressed: GetAnswer,
),
);
}
}
Remove the import import 'dart:html';
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback GetAnswer;
Answer(this.GetAnswer);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.blue,
child: Text("4 CGPA"),
onPressed: GetAnswer,
),
);
}
}

'Function?' can't be assigned to the parameter type 'void Function()?

import 'package:flutter/material.dart';
class ReusableCard extends StatelessWidget {
final Color? colour;
final Widget? cardChild;
final Function? onPress;
ReusableCard({this.colour, this.cardChild, this.onPress});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: **onPress**,
child: Container(
child: cardChild,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0)),
),
);
}
}
lib/reusable_card.dart:15:14: Error: The argument type 'Function?' can't be assigned to the parameter type 'void Function()?'.
'Function' is from 'dart:core'.
onTap: onPress,
^
What could be the error and how should I use the function onPress instead?
Use VoidCallback? instead like so: final VoidCallback? onPress;
Or use final void Function()? onPress;

The argument type 'Function?' can't be assigned to the parameter type 'void Function()?

this is the screenshot of my error code
this code my inputpage:
child: ReusableCard(
onPress: (){
setState(() {
selectGender=Gender.male;
});
},
cardChild: IconContent(icon: FontAwesomeIcons.mars,label: 'Male',),
colour: selectGender==Gender.male? activeCardColor:inactivecolor,
),
and this one other page:onpress function does not work.
class ReusableCard extends StatelessWidget {
ReusableCard({#required this.colour,this.cardChild,this.onPress});
final Color? colour;
final Widget? cardChild;
final Function? onPress;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: cardChild,
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}
i provide screesshoot for your better understand
Because the widget's onTap is defined as a void Function()? onTap, the value needs to be returned to be used (as a callback). So use:
onTap: ()=> onPress,
You can also define the onPress as a VoidCallback?, then you just need to:
onTap: onPress,
change this line
final Function? onPress;
with
final Function() onPress;
class ReusableCard extends StatelessWidget {
ReusableCard({#required this.colour,this.cardChild,this.onPress});
final Color? colour;
final Widget? cardChild;
final Function? onPress;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress, // the problem is here
child: Container(
child: cardChild,
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}
Change this to That:
class ReusableCard extends StatelessWidget {
ReusableCard({#required this.colour,this.cardChild,this.onPress});
final Color? colour;
final Widget? cardChild;
final Function? onPress;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap:(){
--add your on press code here
},
child: Container(
child: cardChild,
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}
Just replace like below.
From
onTap: onPress,
To
onTap: () => onPress,
class ReusableCard extends StatelessWidget {
ReusableCard({#required this.colour,this.cardChild,this.onPress});
final Color? colour;
final Widget? cardChild;
final Function? onPress;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => onPress,
child: Container(
child: cardChild,
margin: EdgeInsets.all(15),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}
I got the same issue, this is the way I solved it:
Instead of
final Function? onPress;
Use this:
final VoidCallback? onPress;

The named parameter 'textColor' isn't defined

I'm getting this error when writing for example color or textColor maybe even with more.
I tried:
flutter clean -> everything is now red, even after restarting. I have to use
'flutter pub get' to get it back to normal
restating PC
ctrl + shift + p -> Dart: Restart Analysis Server
Nothing changes
import 'package:flutter/material.dart';
class Answer extends StatelessWidget {
final VoidCallback selectHandler;
Answer(this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
textColor: Colors.green,
child: Text('Answer 1'),
onPressed: selectHandler,
),
);
}
}
I have no idea what to do and I'm starting to hate this language more and more
Some help would really be appreciated
Do this:
class Answer extends StatelessWidget {
final VoidCallback selectHandler;
Answer(this.selectHandler);
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: ElevatedButton(
child: Text('Answer 1', style: TextStyle(color: Colors.green)),
onPressed: selectHandler,
),
);
}
}

what is the different between Function first class and void function in Dart

in order to optimize my code, I try to use Function first class in the onTap property of the GestureDetector widget, like in the code below:
import 'package:flutter/material.dart';
class ReusableCard extends StatelessWidget {
final Color colour;
final Widget? childCard;
final Function? onPress;
ReusableCard({required this.colour,this.childCard,this.onPress}) ;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: childCard,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: colour,
borderRadius:BorderRadius.circular(10.0),
),
),
);
}
}
but I got that error: The argument type 'Function?' can't be assigned to the parameter type 'void Function()?'.
hope that someone can explain what going on here and thank you previously.
Use VoidCallback? in this case instead of Function?.
In the end, VoidCallback is defined as typedef VoidCallback = void Function() so you can even end up using Function()? but this is a cool shortcut.
You need to use Function()? instead of Function? as the type of onPress variable because onTap requires a GestureTapCallback which is typedef for a Function().
class ReusableCard extends StatelessWidget {
final Color colour;
final Widget? childCard;
final Function()? onPress;
ReusableCard({required this.colour, this.childCard, this.onPress});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
child: childCard,
margin: EdgeInsets.all(15.0),
decoration: BoxDecoration(
color: colour,
borderRadius: BorderRadius.circular(10.0),
),
),
);
}
}