【问题标题】:I/UrlLauncher(17669): component name for (url) is nullI/UrlLauncher(17669): (url) 的组件名称为空
【发布时间】:2022-06-27 18:45:06
【问题描述】:

为什么即使链接存在,它也会抛出错误并给我链接是空的? 当我单独使用启动(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')
            ),
          ),
        ],
      ),

执行热重载

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 计数=1

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您必须将 <queries> 元素添加到您的 AndroidManifest.xml 文件中。 more info

    【讨论】:

    • 在清单中添加 标签并删除 canLaunch(url) 检查解决了我的问题
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      通过链接可以通过其他应用处理,例如 youtube、电子表格、文档...

      从 android 11 (API 30) 及更高版本您必须将此权限添加到 AndroidManifest.xml

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          试试是这样的:

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

          【讨论】:

            【解决方案6】:

            如果您来这里寻找您的电子邮件链接 (mailto:email@example.com) 不起作用的原因,请尝试一下。

            不要为mailto 链接调用canLaunch - 仅用于httphttps

            由于我的应用程序中有http(s)mailto 链接,我使用try-catch 块。

            这是完整的功能:

            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;
                }
              }
            }
            

            【讨论】:

              【解决方案7】:

              您可以使用此代码,它对我有用。 看看:-

              _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';
              }
              

              }

              并在 onPressed() 中使用这个 _launchURL() 函数;

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-05-29
                • 1970-01-01
                • 2014-04-11
                • 1970-01-01
                • 1970-01-01
                • 2023-04-11
                • 1970-01-01
                • 2014-12-26
                相关资源
                最近更新 更多