launchUrl function

Future<bool> launchUrl(
  1. Uri url, {
  2. LaunchMode mode = LaunchMode.platformDefault,
  3. WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
  4. BrowserConfiguration browserConfiguration = const BrowserConfiguration(),
  5. String? webOnlyWindowName,
})

Passes url to the underlying platform for handling.

mode support varies significantly by platform. Clients can use supportsLaunchMode to query for support, but platforms will fall back to other modes if the requested mode is not supported, so checking is not required. The default behavior of LaunchMode.platformDefault is up to each platform, and its behavior for a given platform may change over time as new modes are supported, so clients that want a specific mode should request it rather than rely on any currently observed default behavior.

For web, webOnlyWindowName specifies a target for the launch. This supports the standard special link target names. For example:

  • "_blank" opens the new URL in a new tab.
  • "_self" opens the new URL in the current tab. Default behaviour when unset is to open the url in a new tab.

Web browsers prevent launching URLs in a new tab/window, unless triggered by a user action (e.g. a button click). See package:url_launcher_web for more details.

Returns true if the URL was launched successfully, otherwise either returns false or throws a PlatformException depending on the failure.

Implementation

Future<bool> launchUrl(
  Uri url, {
  LaunchMode mode = LaunchMode.platformDefault,
  WebViewConfiguration webViewConfiguration = const WebViewConfiguration(),
  BrowserConfiguration browserConfiguration = const BrowserConfiguration(),
  String? webOnlyWindowName,
}) async {
  if ((mode == LaunchMode.inAppWebView ||
          mode == LaunchMode.inAppBrowserView) &&
      !(url.scheme == 'https' || url.scheme == 'http')) {
    throw ArgumentError.value(url, 'url',
        'To use an in-app web view, you must provide an http(s) URL.');
  }
  return UrlLauncherPlatform.instance.launchUrl(
    url.toString(),
    LaunchOptions(
      mode: convertLaunchMode(mode),
      webViewConfiguration: convertWebViewConfiguration(webViewConfiguration),
      browserConfiguration: convertBrowserConfiguration(browserConfiguration),
      webOnlyWindowName: webOnlyWindowName,
    ),
  );
}