launchUrl function

Future<void> launchUrl(
  1. String url, {
  2. void onError()?,
})

Launches url in the browser.

This method has special handling for launching URLs when in an embedded VS Code view.

An optional callback onError will be called if url cannot be launched.

Implementation

Future<void> launchUrl(String url, {void Function()? onError}) async {
  final parsedUrl = Uri.tryParse(url);

  try {
    if (parsedUrl != null && await url_launcher.canLaunchUrl(parsedUrl)) {
      await url_launcher.launchUrl(parsedUrl);
    } else {
      onError?.call();
    }
  } finally {
    // Always pass the request up to VS Code because we could fail both silently
    // (the usual behavior) or with another error like
    // "Attempted to call Window.open with a null window"
    // https://github.com/flutter/devtools/issues/6105.
    //
    // In the case where we are not in VS Code, there will be nobody listening
    // to the postMessage this sends.
    launchUrlVSCode(url);
  }
}