Search code examples
flutterurl-launcher

Flutter run url from default browser using url_launcher


I'm using url_launcher (6.1.5) and I want open the urls in default browser instead of in-app webView.

It seems that the latest version does not support it, since launch function is deprecated, and launchUrl does not support the forceWebView=false flag.

How do I run my url on default browser using url_launcher?


Solution

  • TL;DR

    You can use the LaunchMode.externalApplication parameter.

    await launchUrl(_url, mode: LaunchMode.externalApplication);
    

    More info

    By default the mode value is set to LaunchMode.platformDefault, which in most cases is an inAppWebView.

    Changing the mode solved this issue:

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

    LaunchMode support several arguments:

    enum LaunchMode {
      /// Leaves the decision of how to launch the URL to the platform
      /// implementation.
      platformDefault,
    
      /// Loads the URL in an in-app web view (e.g., Safari View Controller).
      inAppWebView,
    
      /// Passes the URL to the OS to be handled by another application.
      externalApplication,
    
      /// Passes the URL to the OS to be handled by another non-browser application.
      externalNonBrowserApplication,
    }