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

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



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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

Solution 4:[4]

With link can handle via other app like youtube, spreadsheets, document...

from android 11 (API 30) and above you must add this permission to AndroidManifest.xml

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

please refer: https://developer.android.com/training/package-visibility/declaring

Solution 5:[5]

Try is like this:

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

Solution 6:[6]

If you come here looking for why your email link (mailto:[email protected]) 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;
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xoltawn
Solution 2 JRJR
Solution 3 Esraa Hamada
Solution 4 C??ng Nguy?n
Solution 5 Huthaifa Muayyad
Solution 6 Aleksandar