MissingPluginException on Flutter



我正试图打开一个URL,但我收到了这个错误,当我在模拟器或设备上运行android应用程序时,屏幕上的结果是什么都没发生,但我得到了"未处理的异常:MissingPluginException";,但当我使用windows应用程序时,效果很好。

launchUrl支持:android,windows,ios

E/flutter(6260):[错误:flutter/runtime/dart_vm_initializer.cc(41)]未处理的异常:MissingPluginException(在通道插件.flutter.io/url_launcher_android上找不到方法启动的实现)E/flutter(6260):#0 MethodChannel_invokeMethod(包:flutter/src/services/platform_channel.dart:294:7)E/颤动(6260):E/flutter(6260):#1 _发送URL(包:peliculas/screens/details_cast.dart:371:8)E/颤振(6260):

我的代码:

Future<void> _launchURL(String url) async {

final Uri uri = Uri(scheme: "https", host: url);

if (!await launchUrl(uri)) {
throw 'Could not launch $url';
}

-我正在使用url=";www.google.com";-模拟器api 30

What I tried before: 
- flutter clean
- flutter pug get 
- flutter run 
- uninstall app 
- flutter doctor -v   is OK
- I'm not using hot restart or hot reload

更新:它发生在其他项目中,但现在只是在这个项目中,我有另一个项目,我只使用这个方法,在更新后,关闭和打开再次开始工作,但主项目没有工作。我把项目转移到另一个地方并打开,但我得到了同样的东西。

解决方案:为了解决这个问题,在我的情况下,url_launcher在我的项目中不起作用,但当我创建一个新的项目时,它运行良好,只需遵循;FirstComment";说,创建一个新项目,将lib文件夹复制到新项目,并像旧项目一样设置Manifest和pubspec配置,还移动其他文件,如asset。

在终端中运行这些命令

1:flutter clean

2:flutter pub get

并重新启动你的应用

更新

您正在使用url_launcher包,该包使用本机设备方法以正常工作,如错误中所示

在通道上未找到方法启动的实现

<application>元素上方的AndroidManifest.xml中添加以下代码

<queries>
<!-- If your app opens https URLs -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<!-- If your app opens https URLs -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent>        
</queries>

<dict>元素内部的Info.plist中添加以下代码

<key>LSApplicationQueriesSchemes</key>
<array>
<string>https</string>
<string>http</string>
</array>

现在,在lib文件夹中创建一个文件launch_url.dart,并粘贴以下代码

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class LaunchUrl extends StatelessWidget {
const LaunchUrl({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Launch Url'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: ElevatedButton(
onPressed:  () async => await _launchUrl('https://google.com/'),
child: const Text('Launch Url'),
),
),
],
),
);
}
Future<void> _launchUrl([
String? url,
]) async {
if (!await launchUrl(Uri.parse(url!), mode: LaunchMode.externalApplication,)) {
throw 'Could not launch $url';
}
}
}

请卸载你手机上的旧应用程序并运行以下命令

颤振清除

flutter pub获取

在此之后,再次运行应用程序,并单击如下所示的按钮在浏览器中打开url。

<img src="https://i.stack.imgur.com/6nl3d.gif" alt="launch url in flutter">

将其添加到您的android清单中

<queries>
<!-- If your app opens https URLs -->
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>

</queries>

最新更新