我想从我的 flutter 应用程序启动 WhatsApp 应用程序 [英] I want to launch WhatsApp application from my flutter application

查看:33
本文介绍了我想从我的 flutter 应用程序启动 WhatsApp 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用此依赖项 url_launcher: ^5.4.1 通过我的 flutter 应用程序启动 whatsapp,但是当我按下按钮启动应用程序时,它不起作用,但在模拟器上显示一条错误消息,无法打开链接.下面给出的是我用来启动 whatsapp 的函数代码.

I am using this dependency url_launcher: ^5.4.1 in my project to launch whatsapp through my flutter application but when i am pressing button to launch application it is not working but showing an error message on emulator that could not open the link. given below is the code with function i am using to launch 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
          
        )
      )
    )
  )

  );
}

}

推荐答案

您的 url 中缺少 https://.

用您的 url() 方法替换下面的代码:

Replace the code below with your url() method:

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

这篇关于我想从我的 flutter 应用程序启动 WhatsApp 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆