Home > Back-end >  'Function?' can't be assigned to the parameter type 'void Function()?
'Function?' can't be assigned to the parameter type 'void Function()?

Time:02-15

    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?

CodePudding user response:

Use VoidCallback? instead like so: final VoidCallback? onPress;

Or use final void Function()? onPress;

  • Related