I'm trying to pass a callback function to a constructor:
//menu.dart
class _MenuState extends State<Menu> {
void callbackFunc() {
// does something...
}
List<Widget> pages = [Home(callback: callbackFunc), Search(), Profile()];
@override
Widget build(BuildContext context) {
...
}
}
//home.dart
class Home extends StatefulWidget {
final Function callback;
Home({ Key? key, required this.callback }) : super(key: key);
@override
State<Home> createState() => _HomeState();
}
But I get this error:
Can't access 'this' in a field initializer to read 'callbackFunc'.
Is there a way to pass the callbackFunc to the Home constructor argument ?