How to Open Phone Dialer with Number in Flutter

ahsan Avatar

·

·

phone dialer flutter

Its always really easy to open to anything in flutter using plugins from pub.dev and that what we are going to do step by step.

Step 1.

add latest version of url_launcher plugin in you pubspec.yaml file in the dependencies section in my cause i will use version 6.1.11 as its latest version available right now.

dependencies:
  url_launcher: ^6.1.11

Step 2.

Now import the url_launcher in the file where you want to use as below:

import 'package:url_launcher/url_launcher.dart';

Step 3.

Now we will define a function to open phone dialer like below:

  void openPhone() async {
    Uri phoneno = Uri.parse('tel:+97798345348734');
    if (await launchUrl(phoneno)) {
      //dialer opened
    } else {
      //dailer is not opened
    }
  }

That it! now you can open phone dialer from you flutter app easily. also there is always room for improvement. for example you can pass phone number as argument. check for Platform specific and run based on current platform you are on it can be Ios, Android, Linux etc.

Full code below: added few improvements as well.

import 'package:url_launcher/url_launcher.dart';

  void openPhone(String phone) async {
    Uri phoneno = Uri.parse('tel:$phone');
    if(await canLaunchUrl(phoneno)){
      await launchUrl(phoneno);
    }else {
      //cannot launch phone
    }
  }

Leave a Reply

Your email address will not be published. Required fields are marked *