I/UrlLauncher(17669): component name for (url) is null

Viewed 9345

Why does it throw an error and give me the link is empty even though the link exists? And when I use launch (url) alone, the link opens without any problems

enter image description here

 String StateUrl = 'View App' ;
 var url = 'https://www.youtube.com/watch?v=-k0IXjCHObw' ;
body: Column(
        children: [
          Text(StateUrl),
          Center(
            child: ElevatedButton.icon(
                onPressed: () async{
                  try {
                    await canLaunch(url) ?
                    await launch(url):
                    throw 'Error';
                  } catch(e){
                    setState(() {
                      StateUrl = e.toString() ;
                    });
                  }
                },
                icon: const Icon(FontAwesomeIcons.link),
                label:  const Text('View Url')
            ),
          ),
        ],
      ),

Performing hot reload

D/EGL_emulation(17669): app_time_stats: avg=17852.65ms min=658.78ms max=35046.52ms count=2 I/UrlLauncher(17669): component name for https://www.youtube.com/watch?v=-k0IXjCHObw is null D/EGL_emulation(17669): app_time_stats: avg=8279.72ms min=8279.72ms max=8279.72ms count=1

8 Answers

You have to add <queries> elements to you AndroidManifest.xml file. more info

try using await launch(url); instead of if (await canLaunch(url)) { print("launching $url"); await launch(url); } else { throw 'Could not launch maps'; } it seems theres a problem with canLaunch(url) function

don't use canLaunch with videos URL just use try/catch

You can use this code, it works for me. Check it out:

_launchURL() async {
  const url = 'https://en.wikipedia.org/wiki/Body_mass_index';
  if (await launch(url)) {
    await canLaunch(url);
  } else {
    throw 'Could not launch $url';
  }
}

and use this _launchURL() function in onPressed();

If you come here looking for why your email link (mailto:email@example.com) doesn't work, then try this out.

Don't call canLaunch for mailto links - use it only for http and https!

Since I have both http(s) and mailto links in my app, I use the try-catch block.

Here is the full function:

class UrlHandler {
  /// Attempts to open the given [url] in in-app browser. Returns `true` after successful opening, `false` otherwise.
  static Future<bool> open(String url) async {
    try {
      await launch(
        url,
        enableJavaScript: true,
      );
      return true;
    } catch (e) {
      log(e.toString());
      return false;
    }
  }
}

Try is like this:

try {
  if(await canLaunch(url)) await launch(url):
   } catch(e){
   setState(() {
   StateUrl = e.toString() ;
    });
throw e;}
},

maybe a little late, but i also had the same problem. The solution was so set an intent in the android manifest file. If this is done, the canLaunch() call will not fail, cause you allow the android system to query this url.

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data  android:scheme="https" android:host="youtube.com" />
    </intent>
</queries>

For comparison, the url launcher now prints following text to the console:

I/UrlLauncher( 2628): component name for <your youtube link> is {com.google.android.youtube/com.google.android.youtube.UrlActivity}

Further if you set the launchMode to LaunchMode.externalApplication the youtube app will launch, if installed.

Hope this helps.

Also Google updates his PolicyBytes and I think using

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

or

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

might lead to app rejects, if you can not explain in detail why you need to use those permissions.

Regards Max.

Related