flutterdart

I was using Flutter's url_launcher package. However, the link is opening in the app itself. I want it to open in my browser


I was using Flutter's url_launcher package. However, the link is opening in the app itself. I want it to open in my browser. Here is my code:

Future<void> launchUrlInBrowser(String url) async {
  Uri urlparsed=Uri.parse(url);
  if (!await launchUrl(urlparsed)) {
    throw Exception('Could not launch $url');
  }
}

Solution

  • The solution I found for the problem is adding an additional parameter to launcUrl( ) method. The parameter is mode: LaunchMode.externalApplication. This parameter will launch the URL in an external app.

    Here is my code fix:

    Future<void> launchUrlInBrowser(String url) async {
      Uri urlparsed=Uri.parse(url);
      if (!await launchUrl(urlparsed,mode: LaunchMode.externalApplication)) {
        throw Exception('Could not launch $url');
      }
    }
    

    EDIT: Flutter now allows you to choose to open the URL in the same tab or a different tab. All you have to do is add an optional string after the mode parameter.

    The strings can be one of the following:

    For example, launchUrl(urlparsed,mode: LaunchMode.externalApplication), webOnlyWindowName: "_self")