dipgirl
dipgirl

Reputation: 690

url_launcher plugin did not work in flutter

I am doing flutter apps. I want to open the website in the app. So, I have used the url_launcher plugin to open the URL but it did not work. There do not have to show the website in the apps.

Here is my code:

class _MainPageState extends State<MainPage>
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    _tabController = new TabController(length: 2, vsync: this);
    super.initState();
  }

  void _launch(String url) async {
    try {
      if (await canLaunch(url)) {
        await launch(url);
      }
    } catch (e) {
      print(e);
    }
  }

  Widget tab1() {
    return Container();
  }

  Widget tab2() {
    return InkWell(
      onTap: () {
        _launch("www.google.com");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          unselectedLabelColor: Colors.black,
          labelColor: Colors.white,
          tabs: [
            new Tab(text: 'Tab 1'),
            new Tab(text: 'Tab 2'),
          ],
          controller: _tabController,
          indicatorColor: Colors.white,
          indicatorSize: TabBarIndicatorSize.tab,
        ),
        bottomOpacity: 1,
      ),
      body: TabBarView(
        children: [tab1(), tab2()],
        controller: _tabController,
      ),
    );
  }
}

Does anyone know how to solve this error?

Upvotes: 0

Views: 1063

Answers (4)

Gert Steenkamp
Gert Steenkamp

Reputation: 617

url_launcher works fine if used correctly

_launchURL() async {
    const url = "https://something.com";
    if (await canLaunch(url)) {
      await launch(
        url,
        forceWebView: false,
        enableJavaScript: true,
        headers: <String, String>{'my_header_key': 'my_header_value'},);
    } else {
      
      throw 'Could not launch ';
    }
  }

The documentation shows when and why to use forceWebView and enableJavaScript.

Note however that the Android 11 (API Level 30) will give you some issues. canLaunch returns false in this case. To make it work on that I had to add the following in my AndroidManifest.xml:

<activity android:name="io.flutter.plugins.urllauncher.WebViewActivity"
           android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
           android:exported="false"/>

That alone did not work so I did some more digging and found this: https://developer.android.com/training/basics/intents/package-visibility#all-apps

I then added the following to my AndroidManifest.xml as well and solved it for me:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

Upvotes: 1

alecsam
alecsam

Reputation: 591

The url should have the proper Scheme:

http: , https:, e.g. http://flutter.dev - Open URL in the default browser

https://pub.dev/packages/url_launcher#supported-url-schemes

Widget tab2() {
  return InkWell(
    onTap: () {
      _launch("https://www.google.com/");
    },
  );
}

Upvotes: 0

Faizan Ahmad
Faizan Ahmad

Reputation: 60

You are using wrong package

To open any website in flutter app there is webview_flutter package Click here to check details to use this package

Upvotes: 2

Hello this your all code in this file?

At first point there are missing some imports at the beginning of the file ;)

For example you need to add

import 'package:url_launcher/url_launcher.dart';

Did you run

pub get

after modyfiying pubspec.yaml file ?

Upvotes: 0

Related Questions