2

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

  • Does this answer your question? [The argument type 'Function' can't be assigned to the parameter type 'void Function()?' after null safety](https://stackoverflow.com/questions/64484113/the-argument-type-function-cant-be-assigned-to-the-parameter-type-void-funct) – Md. Yeasin Sheikh Dec 23 '21 at 09:04

5 Answers5

1

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,
Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
1

change this line

final Function? onPress;

with

final Function() onPress;
q8yas
  • 897
  • 8
  • 10
0
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),
        ),
      ),
    );
  }
}
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),
        ),
      ),
    );
  }
}
KuKu
  • 6,654
  • 1
  • 13
  • 25
0

I got the same issue, this is the way I solved it:

Instead of

final Function? onPress;

Use this:

final VoidCallback? onPress;