How to make a phone call from a flutter app

Viewed 105049

I try to make a phone call from my Flutter app. With the following code:

UrlLauncher.launch('tel: xxxxxxxx');

I found this Function on the GitHub flutter repo: https://github.com/flutter/flutter/issues/4856

But this doesn't work for me. Is this Function still in Flutter and in which package? Or is there a better option to do a phone call from my app?

12 Answers

You should add this in your pubspec.yaml => url_launcher: ^5.0.2 then you click Packages get .

in your code you add the import : import 'package:url_launcher/url_launcher.dart' as UrlLauncher; Hope it works =)

import 'package:url_launcher/url_launcher.dart' as UrlLauncher;


    UrlLauncher.launch('tel:+${p.phone.toString()}')

        //if mail 
     UrlLauncher.launch('mailto:${p.email}'),

This worked for me

use this plugin

import 'package:flutter/material.dart';
    import 'dart:async';
    
    import 'package:flutter/services.dart';
    import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      TextEditingController _numberCtrl = new TextEditingController();
    
      @override
      void initState() {
        super.initState();
        _numberCtrl.text = "085921191121";
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            appBar: new AppBar(
              title: const Text('Plugin example app'),
            ),
            body: new Column(
              children:<Widget>[
                Padding(
                  padding: EdgeInsets.all(8.0),
                  child: TextField(
                    controller: _numberCtrl,
                    decoration: InputDecoration(
                      labelText: "Phone Number"
                    ),
                    keyboardType: TextInputType.number,
                  ),
                ),
                RaisedButton(
                  child: Text("Test Call"),
                  onPressed: () async{
                    FlutterPhoneDirectCaller.callNumber(_numberCtrl.text);
                  },
                )
              ]
            ),
          ),
        );
      }
    }

url_launcher is universal package for launching url, dialing number and sending mail.

  1. Add url_launcher: ^5.5.2 to pubspec.yaml file and run flutter pub get
  2. Import package import 'package:url_launcher/url_launcher.dart';
  3. Define function:
void launchUrl(String url) async {
  if (await canLaunch(url)) {
    launch(url);
  } else {
    throw "Could not launch $url";
  }
}
  1. Call your universal function for different purposes:
//for launching url
launchUrl("HTTP://example.com");

// for dial phone number
launchUrl("tel:+99364921507"); 

// for sending email
launchUrl("mailto:zehinz@gmail.com?subject=Meeting&body=Can we meet via Google Meet"); 

To launch the device's dialer, the following code can also be used with exception handling:

Future<void> launchPhoneDialer(String contactNumber) async {
  final Uri _phoneUri = Uri(
      scheme: "tel",
      path: contactNumber
  );
  try {
    if (await canLaunch(_phoneUri.toString()))
      await launch(_phoneUri.toString());
  } catch (error) {
    throw("Cannot dial");
  }
}

I am able to make a phone call by bringing up the system phone app and CONNECT A CALL:

Here's what you need to do:

  1. pubspec.yaml add package:

    intent:

  2. main.dart:

import 'package:flutter/material.dart';
import 'package:intent/intent.dart' as android_intent;
import 'package:intent/action.dart' as android_action;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      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> {

  @override
  Widget build(BuildContext context) {
    return (Scaffold(
      body: Center(
        child: RaisedButton(
                 onPressed: _launchURL,
                 child: Text('Dial a number'),
               )
      ),
    ));
  }
}


_launchURL() async {
  // Replace 12345678 with your tel. no.

  android_intent.Intent()
    ..setAction(android_action.Action.ACTION_CALL)
    ..setData(Uri(scheme: "tel", path: "12345678"))
    ..startActivity().catchError((e) => print(e));
}

Then, after running this app and click the "Dial a number", the System Phone App will bring up and make a call. (Unlike url_launcher, you don't need to press the Green Call button in the System Phone App)

As on 13th August 2022, these are the latest updates :

This will work for Calls, SMS, Emails and Websites

Step 1:

Go to pubspec.yaml under Project

pubspec.yaml

and paste url_launcher : ^6.1.5 under dependencies:

( Click on https://pub.dev/packages/url_launcher to get the latest version)

> dependencies:   
    flutter:
>     sdk: flutter
> 
>   url_launcher: ^6.1.5

Then Run -

Pub Get

Go to AndroidManifest.xml under android/app/src/main/AndroidManifest.xml

AndroidManifest.xml

And paste the following codes for Phone calls, Sms, Email and Website under package="com.example......" like so -

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.codoweb.your project name">




    <!-- Provide required visibility configuration for API level 30 and above -->
    <queries>
        <!-- If your app opens https URLs -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
        <!-- If your app sends emails -->
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
        <!-- If your app checks for SMS support -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="sms" />
        </intent>
        <!-- If your app checks for call support -->
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="tel" />
        </intent>
    </queries>

Now use the following codes for activating :

    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);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Debasis Sil Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.black,
          ),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.black,
            centerTitle: true,
            title: Text('Debasis Sil Flutter Demo'),
),        
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () async {
                    final web = Uri.parse(
                      'https://codoweb.com/',
                    );
                    if (await canLaunchUrl(web)) {
                      launchUrl(web);
                    } else {
                      throw 'Could not launch $web';
                    }
                  },
                  child: const Text('Web'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final email = Uri(
                      scheme: 'mailto',
                      path: 'codoweb.tech@gmail.com',
                      query: 'subject=Hello&body=Test',
                    );
                    if (await canLaunchUrl(email)) {
                      launchUrl(email);
                    } else {
                      throw 'Could not launch $email';
                    }
                  },
                  child: const Text('Email'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final call = Uri.parse('tel:+91 9830268966');
                    if (await canLaunchUrl(call)) {
                      launchUrl(call);
                    } else {
                      throw 'Could not launch $call';
                    }
                  },
                  child: const Text('Call'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    final sms = Uri.parse('sms:5550101234');
                    if (await canLaunchUrl(sms)) {
                      launchUrl(sms);
                    } else {
                      throw 'Could not launch $sms';
                    }
                  },
                  child: const Text('SMS'),
                ),
              ],
            ),
          ),
        );
      }
    }

And Bingo! You are good to go... Hope this helps :)

I found this motivating that helped me resolve my issue. Please check : https://dev-yakuza.posstree.com/en/flutter/url_launcher/

If you using url_launcher and your phone number have plus symbol like "+1111111111" for ios you should use Uri class

final Uri phoneUrl = Uri(
  scheme: 'tel',
  path: '+11111111111',
);

if (await canLaunch(phoneUrl.toString())) {
  await launch(phoneUrl.toString());
} else {
  throw "Can't phone that number.";
}

Just url_launcher: ^ latest Version in Pubspec.yamal

Note: Before Pub get or Upgrade delete Pubspec.lock some time it gives unwanted problems.

Import package import 'package:url_launcher/url_launcher.dart';

//for launching URL

launchUrl("HTTP://website.com");


// for dial phone number

launchUrl("tel:+91963852741"); 


// for sending email

launchUrl("mailto:mail@gmail.com?subject=Meeting&body=Can we meet via Google Meet"); 

You can call directly by this package flutter_phone_direct_caller

Create a function and pass the mobile number value :

  _callNumber(String mobile) async {
     await FlutterPhoneDirectCaller.callNumber(mobile);
  }

Create a function like this. All above solutions are deprecated with new version.

import URL launch package top of the file

import 'package:url_launcher/url_launcher.dart';

 // Make phone call flutter app
      Future<void> _makePhoneCall(String phoneNumber) async {
        final Uri launchUri = Uri(
          scheme: 'tel',
          path: phoneNumber,
        );
        await launchUrl(launchUri);
      }

Call that functions as same as below. in your action call EX: onPressed Parse your phone number to this function as a string.

_makePhoneCall(phoneNumber);

Add below package to your pub file

url_launcher: ^6.1.5

Add Below Lines to manifest.xml for get permistions

<queries>
    <!-- If your app checks for SMS support -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="sms" />
    </intent>
    <!-- If your app checks for call support -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="tel" />
    </intent>
</queries>

You can do it by two ways:-

  • url_launcher package will be used to implement phone call using the default app of your mobile phone. This package will auto direct the phone to the default phone call making app and open the dialer screen to make the call.

  • flutter_phone_direct_caller package is not the best package but we can implement direct phone calls directly from our phone without the intermediate dialer.

Install the following dependencies inside your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.0
  flutter_phone_direct_caller: ^1.0.1
  url_launcher: ^5.7.10

Making Phone Calls using the "flutter_phone_direct_caller" : plugin To implement the phone call using this plugin is really very easy. This package provides us FlutterPhoneDirectCaller class that provides us callNumber() method that takes a phone number.

_callNumber(String phoneNumber) async {
 String number = phoneNumber;
 await FlutterPhoneDirectCaller.callNumber(number);
}

You can implement it in your button as follows

RaisedButton(
  child: Text("Call"),
  onPressed: () {
    _callNumber(textEditingController.text);
  },

)

Making Phone Calls using the "url_launcher": plugin This package provides us launch(URL) method that takes an URL with schema. Scheme is very essential as it directs the URL. In the case of phone calls, we use ‘tel:’ schema.

_launchPhoneURL(String phoneNumber) async {
  String url = 'tel:' + phoneNumber;
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

and to raised button:-

RaisedButton(
  child: Text("Call"),
  onPressed: () {
    _launchPhoneURL(textEditingController.text);
  },
)

IF you need advanced features you can use "flutter_voip_kit", it is very new library, I have also not used it but it looks promising at first galance..

Related