Dalon
Dalon

Reputation: 700

How to open URL in flutter app with url_launcher?

I use the package url_launcher to open url, here is the code:

  Future <void> _openURL(String url) async{
    if(await canLaunch(url)) {
      await launch(
          url,
        forceSafariVC: false,
        forceWebView: true,
      );
    }
    else{
      throw 'Cant open URL';
    }
  }


ListTile(
                        title: Text('Google'),
                        onTap: () {
                          _openURL('https://www.google.de/');
                        }),

But no matter what url i want to open, i get the error 'Cant open URL'

Upvotes: 0

Views: 12109

Answers (3)

Anil Gupta
Anil Gupta

Reputation: 1225

I was not able to open the URL in Android using Flutter app with pub file https://pub.dev/packages/url_launcher

Since JavaScript was not enable.

Here's working code snippet for Android -

  void _launchURL(String _url) async {
    if (await canLaunch(_url)) {
      await launch(_url, forceSafariVC: true, forceWebView: true, enableJavaScript: true);
    } else {
      throw 'Could not launch $_url';
    }
  }

Then I added a key enableJavaScript: true and it started working on Android too. iOS no need for configuration.

Upvotes: 0

phantomate
phantomate

Reputation: 56

I get the same error: Unhandled Exception: Could not launch

As you can see here https://pub.dev/packages/url_launcher you have to put the following snippet at your AndroidManifest.xml

<queries>
  <!-- If your app opens https URLs -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
</queries>

Upvotes: 4

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14775

Try to below code:

Create launchURL function :

_launch() async {
    const url = 'https://www.google.de/';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

Create your Widget :

ListTile(
     onTap: _launch,
     title: Text('Google'),
    ),

Hope its solved your problem

Upvotes: 1

Related Questions