I have a widget with callback function, but callback function is not called.
My code is as follow:
DetailsRow(
title: "APP",
value: 'MYTEXT',
link: '',
linkTap: () {
print("needs to here"); // this is not getting call
}
),
DetailRow Widget:
class DetailsRow extends StatelessWidget {
const DetailsRow({
super.key,
required this.title,
this.value,
this.link,
this.linkTap
});
final String title;
final String? value;
final String? link;
final VoidCallback? linkTap;
void onlinkTap() async {
if (linkTap != null) {
print('function call1'); // it is call here
linkTap;
} else {
final Uri url = Uri.parse(link!);
await openWebBrowser(url);
}
}
Widget build(BuildContext context) {
return InkWell(
onTap: onlinkTap,
child: Text(
value!,
),
);
}
}
I try with gestureDetector widget as well.
When you simply write the function name like linkTap
you're only referring to it - or in other words, you're not invoking the function.
That's what happening here:
if (linkTap != null) {
print('function call1');
linkTap; // <- This is simply a reference, not calling
}
To invoke a function, you should do functionName()
. Generally speaking, you can should be able to do linkTap()
to call your function. But, since, your linkTap
callback is nullable, this wouldn't work as Dart will show an error.
So the correct solution will be:
if (linkTap != null) {
print('function call1'); // it is call here
linkTap!();
}
Or
linkTap?.call();
Just like pskink commented.
Hope this help you get a better understanding!