我正在使用此依赖项url_launcher:^5.4.1在我的项目中通过我的flutter应用程序启动whatsapp,但当我按下按钮启动应用程序时,它不工作,但在模拟器上显示一条错误消息,无法打开链接。下面给出的是我用来启动whatsapp的函数代码。
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';
void main() => runApp(Wapp());
class Wapp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.orange,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void launchWhatsApp(
{@required int phone,
@required String message,
}) async {
String url() {
if (Platform.isAndroid) {
return "whatsapp://wa.me/$phone:03452121308:/?text=${Uri.parse(message)}";
} else {
return "whatsapp://send? phone=$phone&text=${Uri.parse(message)}";
}
}
if (await canLaunch(url())) {
await launch(url());
} else {
throw 'Could not launch ${url()}';
}
}
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Center(
child: RaisedButton(
color: Colors.orange,
textColor: Colors.black,
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
highlightColor: Colors.green,
onPressed: () {
launchWhatsApp(phone: 03452121308, message: 'Hello');
},
child: Text("Place Your Order",style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15
)
)
)
)
);
}
}
6条答案
按热度按时间a5g8bdjr1#
您的
url
中缺少https://
。将下面的代码替换为您的
url()
方法:ndh0cuux2#
我找到了一个解决方案,将此代码添加到android/app/src/main/AndroidManifest.xml
gcxthw6b3#
djp7away4#
我在
</Application>
Tag下面的android/app/src/main/AndroidManifest.xml
中添加了以下代码:我用来打开Whatsapp的方法:
gudnpqoy5#
插件工作正常,但您需要在android manifest中添加权限,以解决自android 11起的打开问题:https://github.com/flutter/flutter/issues/90099
dnph8jn46#
有两件事对我很有帮助:电话号码应为纯数字。国家/地区代码应为带
+
符号的前缀。比如
+14564564562