launchURL static method

void launchURL(
  1. String url, {
  2. bool openInApp = true,
  3. bool clearCache = false,
  4. bool clearCookies = false,
  5. bool sameTab = false,
})

Implementation

static void launchURL(String url, {bool openInApp = true, bool clearCache = false, bool clearCookies = false, bool sameTab = false}) async {
  // Ensure URL has protocol — without it, canLaunchUrl and launchUrl fail
  if (url.isNotEmpty && !url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('mailto:') && !url.startsWith('tel:')) {
    url = 'https://$url';
  }

  AppConfig.logger.d('Launching: $url - openInApp: $openInApp');

  try {
    if (await canLaunchUrl(Uri.parse(url))) {

      if(openInApp && UrlUtilities.isExternalDomain(url)) {
        openInApp = false;
      }

      if (kIsWeb) {
        // Web: open in new tab (default) or same tab
        await launchUrl(Uri.parse(url),
          mode: LaunchMode.externalApplication,
          webOnlyWindowName: sameTab ? '_self' : '_blank',
        );
      } else {
        if(clearCache) await clearWebViewCache();
        if(clearCookies) await clearWebViewCookies();

        await launchUrl(Uri.parse(url),
          mode: openInApp ? LaunchMode.inAppWebView : LaunchMode.externalApplication,
        );
      }

    } else {
      AppConfig.logger.i('Could not launch $url');
    }
  } catch(e, st) {
    NeomErrorLogger.recordError(e, st, module: 'neom_commons', operation: 'launchURL');
  }
}