gpt4 book ai didi

android - 我想从我的 flutter 应用程序启动 WhatsApp 应用程序

转载 作者:行者123 更新时间:2023-12-03 23:26:39 25 4
gpt4 key购买 nike

我在我的项目中使用此依赖项 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

)
)
)
)

);
}

}

最佳答案

您缺少 https://在您的 url .
将下面的代码替换为您的 url()方法:

  String url() {
if (Platform.isAndroid) {
// add the [https]
return "https://wa.me/$phone/?text=${Uri.parse(message)}"; // new line
} else {
// add the [https]
return "https://api.whatsapp.com/send?phone=$phone=${Uri.parse(message)}"; // new line
}
}

关于android - 我想从我的 flutter 应用程序启动 WhatsApp 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63312503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com