import 'package:url_launcher/link.dart';
How to call followLink
and launchUrl()
child: Link(
target: LinkTarget.blank,
uri: Uri.parse('https://www.google.com'),
builder: (context, followLink) =>
ElevatedButton(
onPressed: followLink,
style: ElevatedButton.styleFrom(primary: constants.appBarColor),
child: Container(
width: MediaQuery.of(context).size.width,
height: SizeConfig.safeBlockHorizontal * 4.5,
child: Center(
child: Text(
'Visit Link',
style: TextStyle(
fontSize: SizeConfig.safeBlockHorizontal * 4.05
),
),
),
),
),)
I need to call this method launchUrl()
after clicking onPress
Future launchUrl() async{
await Future.delayed(const Duration(milliseconds: 900)).whenComplete(() => {
showToastWidget('Link clicked', Colors.green),
callPointsPerClick();
});}
I tried this code below the launchUrl()
is working but the followLink
is not
onPressed: (){
followLink;
launchUrl();}
CodePudding user response:
use following code :
onPressed: (){
followLink();
launchUrl();
}
CodePudding user response:
TL;DR
Modify your snippet to add parentheses at the end of followLink;
line before the semicolon ;
to actually call the method like the following:
onPressed: (){
followLink();
launchUrl();
}
In Details
When you type a method name only without the parentheses that means you are only referencing it and when you add the parentheses to the name that means you are calling it.
When the code was like this:
onPressed: followLink,
It worked because you are telling the ElevatedButton
to take the reference of the followLink
method to call it later when the onPressed
occur, so you are just pointing the onPressed
to the followLink
to call it later.
While when you changed the code to be the following:
onPressed: (){
followLink;
launchUrl();
}
It did not work because here you don't need to pass a reference for the method, you need to actually call it because the callback reference is the anonymous method it self and the body of the method is the actually instructions to be performed.
Accoring to that, the line followLink;
actually means nothing and you should be seeing a warning in the editor to remove it and the line launchUrl();
worked because you are actually using the parentheses to call the function.
So your ideal is correct but you have to slightly modify your snippet to be the following according to the mentioned reason:
onPressed: (){
followLink();
launchUrl();
}