Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

calls
flutter
communitycreator

How to make calls in Flutter

Irzum Jafri

Flutter is a powerful tool to create applications with all kinds of functionality. It develops multi-platform applications. However, its largest libraries are phone-specific features that help add functionality to the application.

Such libraries include the flutter_phone_direct_caller and the url_launcher packages, which effectively implement call functionalities within your application.

The two different packages that are mentioned above provide the calling feature according to your need. Their specific functionalities are as follows:

  • flutter_phone_direct_caller: This package implements a direct calling feature inside your application and dials the number within the app.

  • url_launcher: This package redirects calls from your application to the phone’s dialer application to make calls.

flutter_phone_direct_caller

To use this package, you will first need to import it into your application. To do so, add the dependency into your flutter project by adding the following to your pubspec.yaml file:

flutter_phone_direct_caller: ^2.1.0

After this, run the command flutter pub get in your terminal to import any required files and libraries.

Once the package is ready to use, add the plugin to your application. To do so, call the following function:

number = '031186XXXXX'
await FlutterPhoneDirectCaller.callNumber(number);
// number is the phone number you wish to dial
// This would instantly dial a call to the required number.

You can find the full application code here:

import 'package:flutter/material.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';

void main() {
  runApp(Scaffold(
    body: Button(
        onPressed: callNumber,
        child: Text('Call Number'),
      ),
  ));
}

callNumber() async{
  const number = '031186XXXXX'; //set the number here
  bool call = await FlutterPhoneDirectCaller.callNumber(number);
}

An alternative method to implement calling in your application is the indirect method which you can find below:

url_launcher

Similar to the previous plugin, you will first have to add the dependency in the pubspec.yaml file. After this, you will run flutter pub get to import the plugin into your application.

Alternatively, you can also run the flutter pub add url_launcher for it to be directly added without any other changes needed.

dependencies:
  url_launcher: ^6.0.12

You can then use the function below in your application to redirect to your phone dialer and make calls:

import 'package:url_launcher/url_launcher.dart';

launchDialer(String number) async {
  String url = 'tel:' + number;
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Application unable to open dialer.';
  }
}

You can find the complete app code here:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

String number = 0311863XXXX
void main() {
  runApp(Scaffold(
    body: Button(
        onPressed: launchDialer(number),
        child: Text('Open Dialer'),
      ),
  ));
}

launchDialer(String number) async {
  String url = 'tel:' + number;
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Application unable to open dialer.';
  }
}

RELATED TAGS

calls
flutter
communitycreator
RELATED COURSES

View all Courses

Keep Exploring