'Adding onPressed function from class variable

I got this error when I try to pass the onPressed value from a variable inside list of items .

    class Raccourcis {
  final String imgSrc, title;
  final Function() onTapAction;
  BuildContext context;

  Raccourcis({this.imgSrc, this.title, this.onTapAction, this.context, info});
}

List listeRaccourcis = [
  Raccourcis(
      title: "Ajouter Un Bon de commande",
      imgSrc: "assets/images/commande.png",
      onTapAction: () {
        print("Passed");
        BuildContext context;
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Test()),
        );
      }),
      ];

and this is how I'm willing to use my list in another stageful Widgetand the onPressed values.

child: MaterialButton(
    onPressed: widget.info.onTapAction,

In the console I'm getting this error :

TypeError: Cannot read properties of null (reading 'findAncestorStateOfType')


Solution 1:[1]

class Raccourcis {
  final String imgSrc, title;
  // final Function() onTapAction; // replace Function() with VoidCallback?
  final VoidCallback? onTapAction;
  BuildContext context;

  Raccourcis({this.imgSrc, this.title, this.onTapAction, this.context, info});
}

List listeRaccourcis = [
  Raccourcis(
      title: "Ajouter Un Bon de commande",
      imgSrc: "assets/images/commande.png",
      onTapAction: () {
        print("Passed");
        BuildContext context;
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => Test()),
        );
      }),
      ];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mouaz M Shahmeh