So I want to trigger a function out of a child class in flutter. This is the part where the function should be triggered
onTap: onItemTap,
I tried passing the function on like this:
Parent:
bottomNavigationBar: BottomBar(onItemTap(_selectedIndex))
Function in parent:
void _onItemTap(int index) {
setState(() {
_selectedIndex = index;
});
}
Child:
final void Function(int _selectedIndex) onItemTap;
const BottomBar( this.onItemTap);
But I get this error in my Parent:
This expression has a type of 'void' so its value can't be used
How do I pass the function on properly?
CodePudding user response:
You can use _onItemTap
as a tear-off directly, just do:
bottomNavigationBar: BottomBar(_onItemTap)
The BottomBar
expects a void Function(int _selectedIndex)
which literally means A void function that takes one parameter that is an int since _onItemTap
is exactly that, it can be passed directly.