Search code examples
flutterdartasynchronousurl-launcher

Unhandled Exception when using url launcher in flutter


RichText(
    text: TextSpan(
      children: [
        TextSpan(
          text: "Soumik's LinkedIn Profile",
          style:
              TextStyle(fontSize: 20.0, color: Colors.blue),
          recognizer: TapGestureRecognizer()
            ..onTap = () async {
              var url =
                  "https://www.linkedin.com/in/soumik-mukherjee-438b451b5/";
              if (await canLaunch(url)) {
                await launch(url);
              } else {
                throw "Failed to open LinkedIn";
              }
            },
        )
      ],
    ),
  ),

I wrote this code on flutter to launch the URL of my LinkedIn profile. When I run this on an emulator, it works fine as the webpage opens automatically when I tap on the URL. But, when I tried the same on a physical device by running the app in debug mode, the URL does not respond. Instead, I get an error in the console.

THIS IS THE SCREENSHOT OF THE ERROR IN THE TERMINAL


Solution

  • I assume you have Android 11 installed on your POCO X2, which is API level 30.

    Here is the documentation for the canLaunch function: https://pub.dev/documentation/url_launcher/latest/url_launcher/canLaunch.html.

    The documentation says: "On Android (from API 30), canLaunch will return false when the required visibility configuration is not provided in the AndroidManifest.xml file. For more information see the Managing package visibility article in the Android docs."

    Therefore I assume that you did not provide the visibility configuration in the AndroidManifest.xml file which causes canLaunch to return false.