url_launcher#launchUrl() returns false on chrome and edge in flutter web project

242 views Asked by At

Tested in debug and in production on Chrome and Edge.

My flutter web project throws an Exception after I click the code (< >) IconButton in the upper-right corner. You may be able to confirm in your devtools console. The Exception should only be thrown if url_launcher fails to launch the url... but the new tab opens just fine.

This is how I'm calling url_launcher#launchUrl()

Future<void> _launchUrl(String urlString) async {
  if (!await launchUrl(Uri.parse(urlString))) {
    throw Exception("Could not launch $urlString");
  }
}

This method is called from the AppBar actions

appBar: AppBar(
  backgroundColor: Theme.of(context).colorScheme.primaryContainer,
  title: Text(widget.title),
  actions: [
    IconButton(onPressed: () => setState(() {
      _launchUrl("https://gitlab.com/friend.code");
    }), icon: const Icon(Icons.code)),
    IconButton(onPressed: () {}, icon: const Icon(Icons.person)),
    IconButton(onPressed: () {}, icon: const Icon(Icons.music_note))
  ],
),

This is ~exactly as written in the url_launcher example.

I was able to debug the url_launcher#launchUrl() execution up to url_launcher_web#openNewWindow(), which calls _window.open(url, target, 'noopener, noreferrer');. I am unable to step into that method, so not sure what is happening there, but this method call returns null.

Why am I getting a seemingly false-negative result here?

Full source code

webOnlyWindowName

If I set webOnlyWindowName: "_self", then the link opens in the current tab and I do not see any console error. If I set webOnlyWindowName: "_blank", then I still get the console error even though the new tab opens as intended. The docs say that launchUrl "Returns true if the URL was launched successful, otherwise either returns false or throws a PlatformException depending on the failure." I just want to know why it's returning false.

created GitHub issue

https://github.com/flutter/flutter/issues/139783

1

There are 1 answers

1
PradeepKumar On

In my case i have used

kIsWeb?html.window.open(
  "https://gitlab.com/friend.code",
  "GitLab",
): _launchUrl("https://gitlab.com/friend.code");

or

 await launchUrl(
      Uri.parse(url),
      webOnlyWindowName: isNewTab ? '_blank' : '_self',
    );