Home > Software engineering >  Function in flutter
Function in flutter

Time:11-16

I try to use a function in a widget but i recive this error Error: The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.

I search here and see that someone suggest to use the same metod that I set in my code so what's the problem?

import 'package:flutter/material.dart';

class ReusableCard extends StatelessWidget {
  ReusableCard(
      {required this.colour, required this.cardChild, required 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.0),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.0),
          color: colour,
        ),
      ),
    );
  }
}

CodePudding user response:

change final Function onPress; to final Function() onPress;

CodePudding user response:

Replace onTap: onPress with onTap: () => onPress()

  • Related