I/UrlLauncher(17669):(url) 的组件名称为空

Moh*_*nas 26 dart flutter

为什么它会抛出错误并提示我链接为空,即使链接存在?当我单独使用 launch (url) 时,链接打开没有任何问题。

在此输入图像描述

 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')
            ),
          ),
        ],
      ),
Run Code Online (Sandbox Code Playgroud)

执行热重载:

D/EGL_emulation(17669): app_time_stats: avg=17852.65ms min=658.78ms max=35046.52ms count=2 I/UrlLauncher(17669): https://www.youtube.com/watch?v=-k0IXjCHObw的组件名称 为空 D/EGL_emulation(17669): app_time_stats: avg=8279.72ms min=8279.72ms max=8279.72ms count=1

Xol*_*awn 48

您必须将<queries>元素添加到AndroidManifest.xml文件中。 更多信息

  • 在清单中添加 &lt;queries/&gt; 标签并删除 canLaunch(url) 检查为我解决了这个问题 (2认同)

Cươ*_*yễn 19

通过链接可以通过其他应用程序(例如 YouTube、电子表格、文档)进行处理...

从 android 11 (API 30) 及以上版本您必须添加此权限AndroidManifest.xml

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
Run Code Online (Sandbox Code Playgroud)

或者询问您需要什么:

<queries>
   <package android:name="net.com.example" /> // your android package name
</queries>
Run Code Online (Sandbox Code Playgroud)

更新: 对于 QUERY_ALL_PACKAGES,您在发布应用程序时需要向 Google Play 方面解释。

为了避免这种情况,您需要查询您需要的内容: package, intent...

请参考: https: //developer.android.com/training/package-visibility/declaring

  • 此解决方案有效,但当您在 ​​Google Play 上发布时,它会产生权限问题,您必须先对其进行管理,然后才能发布应用程序。@JRJR 更好的解决方案。您可以将其添加到 https 架构的清单中: &lt;queries&gt; &lt;intent&gt; &lt;action android:name="android.intent.action.VIEW" /&gt; &lt;category android:name="android.intent.category.BROWSABLE" / &gt; &lt;data android:scheme="https" /&gt; &lt;/intent&gt; &lt;/queries&gt; (3认同)

小智 16

尝试使用 await launch(url); 而不是 if (await canLaunch(url)) { print("launching $url"); await launch(url); } else { throw 'Could not launch maps'; } canLaunch(url) 函数似乎有问题


小智 9

不要canLaunch与视频 URL 一起使用,只需使用try/catch.