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 ofFunction?.In the end,
VoidCallbackis defined astypedef VoidCallback = void Function()so you can even end up usingFunction()?but this is a cool shortcut.