【问题标题】:UrlLauncher( 9700): component name for mailto is nullUrlLauncher(9700):mailto 的组件名称为空
【发布时间】:2022-01-11 12:52:03
【问题描述】:

所以我知道对于 android 版本 >= 30,您需要在清单中添加查询,但这对我没有用。 我使用的是 Android 版本 29。不过,我还是将这些行添加到了我的清单中。

所以我的清单看起来像这样。

 <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

我设法用下面的代码重现了我的机器的错误。不过我得到了这个错误:

I/UrlLauncher(10601):mailto:?subject=&body= 的组件名称为空

您可以在下面找到 main.dart 的代码

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {return Scaffold(
      body: Center(
        child: ElevatedButton(
          style: ButtonStyle(
            shape: MaterialStateProperty.all<RoundedRectangleBorder>(
              RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(18.0)),
            ),
          ),
          onPressed: () => launchEmail(
            toEmail: "js",
            subject: "dm",
            message: "jd",
          ), child: Text("null"),
        ),
      ),
    );
  }

  Future launchEmail({
    required String toEmail,
    required String subject,
    required String message,
  }) async {
    final url =
        "mailto:$toEmail?subject=${Uri.encodeFull(subject)}&body=${Uri.encodeFull(message)}";
    if (await canLaunch(url)) {
      await launch(url);
    }
  }
}

【问题讨论】:

  • 所以我遇到了类似的错误,我刚刚添加了我想使用的应用程序的包名称&lt;package android:name="com.app" /&gt; 你还在调试清单中添加了它而不是发布吗?
  • 我将我的 Android API 版本更新到 30+,现在它可以工作了????????

标签: flutter android-studio dart url-launcher


【解决方案1】:

我做了什么来解决这个错误:

我将我的 Android API 设置为 30+

您的 manifest.xml 应包含以下内容:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.DIAL" />
        <data android:scheme="tel" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

如果这不能为您解决问题,Suj 建议添加:

<package android:name="com.app" />

到你的清单

【讨论】:

    【解决方案2】:

    通过将您的 API 更新为 30 (minSdkVersion),您将限制可以安装您的应用的设备和 Android 版本。

    替代解决方案:不要为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;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-27
      • 2010-12-18
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2022-06-20
      相关资源
      最近更新 更多