How to make a phone call from a Flutter app

In this article, we will learn how to make a phone call from a Flutter app. This is a common requirement in many apps where users need to be able to call someone directly from the app.

The Code

We can use the url_launcher package to make a phone call from our Flutter app. Here’s an example code snippet that demonstrates how to do it:

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new Home(),
    );
  }
}

class Home extends StatelessWidget {
  Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) => new Scaffold(
        appBar: new AppBar(
          title: new Text("View"),
        ),
        body: new Center(
          child: new FlatButton(
              onPressed: () => launchUrlString("tel://21213123123"),
              child: new Text("Call me")),
        ),
      );
}

void main() {
  runApp(
    new MyApp(),
  );
}

We can also import the url_launcher package and use it directly:

import 'package:url_launcher/url_launcher.dart' as UrlLauncher;
UrlLauncher.launchUrlString("tel://21213123123")

The Steps

To make a phone call from our Flutter app, we need to follow these steps:

Step 1: Add the url_launcher package to your pubspec.yaml file

We need to add the url_launcher package to our pubspec.yaml file. Here’s how to do it:

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.1.5

Step 2: Run Pub Get

After adding the package, we need to run Pub Get to install it:

flutter pub get

Step 3: Add the necessary permissions to your AndroidManifest.xml file

We also need to add the necessary permissions to our AndroidManifest.xml file. Here’s how to do it:


    ...
    
        
        
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        
            
            
        
    

Step 4: Use the launchUrlString function to make a phone call

Finally, we can use the launchUrlString function from the url_launcher package to make a phone call:

launchUrlString("tel://21213123123")

The Full Code

Here’s the full code snippet that demonstrates how to make a phone call from our Flutter app:

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new Home(),
    );
  }
}

class Home extends StatelessWidget {
  Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) => new Scaffold(
        appBar: new AppBar(
          title: new Text("View"),
        ),
        body: new Center(
          child: new FlatButton(
              onPressed: () => launchUrlString("tel://21213123123"),
              child: new Text("Call me")),
        ),
      );
}

void main() {
  runApp(
    new MyApp(),
  );
}

Conclusion

In this article, we learned how to make a phone call from a Flutter app. We used the url_launcher package and followed four steps to achieve this. I hope this helps!