launchUrl function Null safety

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

Passes url to the underlying platform for handling.

mode support varies significantly by platform:

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.

Returns true if the URL was launched successful, 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(),
  String? webOnlyWindowName,
}) async {
  final bool isWebURL = url.scheme == 'http' || url.scheme == 'https';
  if (mode == LaunchMode.inAppWebView && !isWebURL) {
    throw ArgumentError.value(url, 'url',
        'To use an in-app web view, you must provide an http(s) URL.');
  }
  // TODO(stuartmorgan): Use UrlLauncherPlatform directly once a new API
  // that better matches these parameters has been added. For now, delegate to
  // launchUrlString so that there's only one copy of the parameter translation
  // logic.
  return await launchUrlString(
    url.toString(),
    mode: mode,
    webViewConfiguration: webViewConfiguration,
    webOnlyWindowName: webOnlyWindowName,
  );
}