How can I dial the phone from Flutter? - flutter

I am building a Flutter app, and I'd like to dial a phone number in response to a button tap. What's the best way to do this?
Thanks!

This method will open the dialer :
_launchCaller() async {
const url = "tel:1234567";
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
EDIT:
In case anybody facing errors:
Add url_launcher: in the pubspec.yaml & run flutter get
Also import 'package:url_launcher/url_launcher.dart';

Typically, to interact with the underlying platform, you have to write platform specific code and communicate with the same using platform channels. However, Flutter provides some points of integration with the platform out of the box. To dial the phone for instance, you can use the UrlLauncher.launch API with the tel scheme to dial the phone.
Something like UrlLauncher.launch("tel://<phone_number>"); should work fine on all platforms.
Do note that this will not work in the simulators. So make sure you are using an actual device to test this.

You can use the url_launcher widget (https://pub.dev/packages/url_launcher)
Add this to your package's pubspec.yaml file: dependencies: url_launcher: ^5.7.10
Install it: $ flutter pub get
Import it import 'package:url_launcher/url_launcher.dart';
Inside your class, define this method so you can call from any action in your code:
Future<void> _makePhoneCall(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
inside the build widget:
IconButton(icon: new Icon(Icons.phone),
onPressed: ()
{
setState(() {
_makePhoneCall('tel:0597924917');
});
},
),
Note 1: you should write the phone number with prefix 'tel': 'tel:0123456789'
Note 2: sometimes it will not work well until you close the app in your mobile and reopen it, so flutter can inject the code of the new widget successfully.

Its too easy
import 'package:flutter/material.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
void main() {
runApp(Scaffold(
body: Center(
child: RaisedButton(
onPressed: _callNumber,
child: Text('Call Number'),
),
),
));
}
_callNumber() async{
const number = '08592119XXXX'; //set the number here
bool res = await FlutterPhoneDirectCaller.callNumber(number);
}

URL launcher has updated their dependency so here is the updated way to do the job. it will work for android ios
final Uri launchUri = Uri(
scheme: 'tel',
path: number,
);
await launchUrl(launchUri);
Who has not used this plugin just imoprt this plugin.
UrlLauncher

Use url_launcher.
import 'package:url_launcher/url_launcher.dart';
openDialPad(String phoneNumber) async {
Uri url = Uri(scheme: "tel", path: phoneNumber);
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
print("Can't open dial pad.");
}
}
Note: Don't forgot to update info.plist and AndroidManifest.xml as per the documentation.

Follow the below steps:
add the url_launcher: Latest version dependency in your pubspec.yaml
Go to \android\app\src\main\AndroidManifest.xml.
add the queries lines before the <application, like this :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter">
<queries>
<intent>
<action android:name="android.intent.action.DIAL" />
<data android:scheme="tel" />
</intent>
</queries>
<application ...
For more queries, follow this more queries
4.Add the dialer function, and set the url, final url ="tel:$phoneNumber"; like this:
Future<void> dialNumber(
{required String phoneNumber, required BuildContext context}) async {
final url = "tel:$phoneNumber";
if (await canLaunch(url)) {
await launch(url);
} else {
ShowSnackBar.showSnackBar(context, "Unable to call $phoneNumber");
}
return;
}
Done

This is what worked for me as of January 2023
NB: The major difference from the other answers above is my #4
ALSO: You can now use test this in Emulator
Steps:
1.Add url_launcher: latest to pubspec.yaml
2.Configuration:
For IOS: Add tel scheme passed to canLaunchUrl as LSApplicationQueriesSchemes entries in your Info.plist file.
Like this:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tel</string>
</array>
For Android: Add tel scheme passed to canLaunchUrl as queries entries in your AndroidManifest.xml [ie \android\app\src\main\AndroidManifest.xml] file.
Just like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutApp">
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="tel" />
</intent>
</queries>
<application
3.Import url_launcher to your file: ie import 'package:url_launcher/url_launcher.dart';
4.Code
Future<void> _dialNumber(String phoneNumber) async {
final Uri launchUri = Uri(
scheme: 'tel',
path: phoneNumber,
);
await launchUrl(launchUri);
}
Source: url_launcher

Related

I/UrlLauncher(17669): component name for (url) is null

Why does it throw an error and give me the link is empty even though the link exists?
And when I use launch (url) alone, the link opens without any problems
String StateUrl = 'View App' ;
var url = 'https://www.youtube.com/watch?v=-k0IXjCHObw' ;
body: Column(
children: [
Text(StateUrl),
Center(
child: ElevatedButton.icon(
onPressed: () async{
try {
await canLaunch(url) ?
await launch(url):
throw 'Error';
} catch(e){
setState(() {
StateUrl = e.toString() ;
});
}
},
icon: const Icon(FontAwesomeIcons.link),
label: const Text('View Url')
),
),
],
),
Performing hot reload
D/EGL_emulation(17669): app_time_stats: avg=17852.65ms min=658.78ms
max=35046.52ms count=2 I/UrlLauncher(17669): component name for
https://www.youtube.com/watch?v=-k0IXjCHObw is null
D/EGL_emulation(17669): app_time_stats: avg=8279.72ms min=8279.72ms
max=8279.72ms count=1
You have to add <queries> elements to you AndroidManifest.xml file.
more info
try using
await launch(url);
instead of
if (await canLaunch(url)) { print("launching $url"); await launch(url); } else { throw 'Could not launch maps'; }
it seems theres a problem with canLaunch(url) function
With link can handle via other app like youtube, spreadsheets, document...
from android 11 (API 30) and above you must add this permission to AndroidManifest.xml
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
please refer:
https://developer.android.com/training/package-visibility/declaring
don't use canLaunch with videos URL just use try/catch
If you come here looking for why your email link (mailto:email#example.com) doesn't work, then try this out.
Don't call canLaunch for mailto links - use it only for http and https!
Since I have both http(s) and mailto links in my app, I use the try-catch block.
Here is the full function:
class UrlHandler {
/// Attempts to open the given [url] in in-app browser. Returns `true` after successful opening, `false` otherwise.
static Future<bool> open(String url) async {
try {
await launch(
url,
enableJavaScript: true,
);
return true;
} catch (e) {
log(e.toString());
return false;
}
}
}
You can use this code, it works for me. Check it out:
_launchURL() async {
const url = 'https://en.wikipedia.org/wiki/Body_mass_index';
if (await launch(url)) {
await canLaunch(url);
} else {
throw 'Could not launch $url';
}
}
and use this _launchURL() function in onPressed();
Try is like this:
try {
if(await canLaunch(url)) await launch(url):
} catch(e){
setState(() {
StateUrl = e.toString() ;
});
throw e;}
},
maybe a little late, but i also had the same problem. The solution was so set an intent in the android manifest file. If this is done, the canLaunch() call will not fail, cause you allow the android system to query this url.
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" android:host="youtube.com" />
</intent>
</queries>
For comparison, the url launcher now prints following text to the console:
I/UrlLauncher( 2628): component name for <your youtube link> is {com.google.android.youtube/com.google.android.youtube.UrlActivity}
Further if you set the launchMode to LaunchMode.externalApplication the youtube app will launch, if installed.
Hope this helps.
Also Google updates his PolicyBytes and I think using
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
or
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
might lead to app rejects, if you can not explain in detail why you need to use those permissions.
Regards Max.
The url_launcher requires a Uri object to be passed instead of a string. Add a Uri.parse
String StateUrl = 'View App' ;
var url = 'https://www.youtube.com/watch?v=-k0IXjCHObw' ;
body: Column(
children: [
Text(StateUrl),
Center(
child: ElevatedButton.icon(
onPressed: () async{
try {
Uri uri = Uri.parse(url);
await canLaunch(uri) ?
await launch(uri):
throw 'Error';
} catch(e){
setState(() {
StateUrl = e.toString() ;
});
}
},
icon: const Icon(FontAwesomeIcons.link),
label: const Text('View Url')
),
),
],
),
for me the solution is
to copy and paste
<!-- Provide required visibility configuration for API level 30 and above -->
<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>
from official packages docs
but the problem is that the package removed the next lines from code snippet
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
so add them first to the
I just used this and it worked...(nb:Dec 2022)
if (!await launchUrl(url)) {
throw 'Could not launch $url';
}
Try using await launch(url); instead of
if (await canLaunch(url)) {
print("launching $url");
await launch(url);
}
else {
throw 'Could not launch maps';
}
It seems theres a problem with canLaunch(url) function
Thank you for this Solution :)

flutter + firebase passwordless email login - dynamic link data returns null

I am trying to implement passwordless email login on my app.
After the link has been clicked on, the app returns from the background but the dynamic link is null.
This is how I send the mail (with the right values for my app of course):
user.sendSignInWithEmailLink(
email: _email,
androidInstallIfNotAvailable: true,
iOSBundleID: "com.company.appname",
androidMinimumVersion: "16",
androidPackageName: "com.company.appname",
url: "https://appname.page.link/email",
handleCodeInApp: true);
I also added the intent as follows:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="appname.page.link" android:scheme="http"/>
<data android:host="appname.page.link" android:scheme="https"/>
</intent-filter>
Tried different variations with the intent, and non helped, including writing the firebase
project url as the host like: project-name.firebaseapp.com
The data still comes back empty.
Any thoughts? am I missing something?
I've updated the firebase_dynamic_links package to the latest version (0.5.0+9),
while using the configuration below and it starts working.
Sending sign-in link configuration:
firebaseAuth.sendSignInWithEmailLink(
email: email,
url: "https://mydemoapp.page.link/email",
androidInstallIfNotAvailable: true,
androidMinimumVersion: '21',
androidPackageName: 'com.example.mydemoapp'
handleCodeInApp: true,
iOSBundleID: 'com.example.mydemoapp');
AndroidManifest.xml intent-filter configuration:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:host="mydemoapp.page.link" android:scheme="https"/>
<data android:host="mydemoapp.page.link" android:scheme="http"/>
</intent-filter>
Getting a dynamic link in Flutter.
The example comes form from https://pub.dev/packages/firebase_dynamic_links
If your app did not open from a dynamic link, getInitialLink() will return null. That's the reason why you have to FirebaseDynamicLinks.instance.onLink implemented in case the app is already opened.
void main() {
runApp(MaterialApp(
title: 'Dynamic Links Example',
routes: <String, WidgetBuilder>{
'/': (BuildContext context) => MyHomeWidget(), // Default home route
'/helloworld': (BuildContext context) => MyHelloWorldWidget(),
},
));
}
class MyHomeWidgetState extends State<MyHomeWidget> {
.
.
.
#override
void initState() {
super.initState();
this.initDynamicLinks();
}
void initDynamicLinks() async {
final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
if (deepLink != null) {
Navigator.pushNamed(context, deepLink.path);
}
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;
if (deepLink != null) {
Navigator.pushNamed(context, deepLink.path);
}
},
onError: (OnLinkErrorException e) async {
print('onLinkError');
print(e.message);
}
);
}
.
.
.
}
The mydemoapp.page.link must be added to Authorized domains in Firebase console
Is it appname.page.link or appname.page.link/email you have to specify only one email in order to achieve that
If coming to this answer in March 2022, this error is happening again and it's a bug on Flutterfire.
Here is the issue with a Pull Request that hasn't been merged yet.

How to send email with flutter?

I tried all packages to send email ( flutter_email_sender - flutter_mailer -url_launcher), copy and paste the example, but always the same error message : " MissingPluginException(No implementation found for methode ...), I serach a simple example to send email on press button.
thank you
To use latest version of url_launcher or above version of 4.1.0+1
, you have to migrate to android x.
[https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility][1]
Example:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class SendEmail extends StatelessWidget {
void _contact() async {
final url = 'mailto:dude#gmail.com';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
#override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: (){_contact()},
child: Text('Mail'),
),
}
}
try adding these lines on AndroidManifest.xml
<!-- Provide required visibility configuration for API level 30 and above -->
<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>
then run flutter clean, then build again
it work for me but this is specific for url_launcher

Flutter email sender

I have this error when I send an email from form in flutter.
Unhandled Exception: PlatformException(UNAVAILABLE, defualt mail app not available, null)
class _MyAppState extends State<MyApp> {
List<String> attachment = <String>[];
TextEditingController _subjectController =
TextEditingController(text: 'ct');
TextEditingController _bodyController = TextEditingController(
text: ''' a
''');
final GlobalKey<ScaffoldState> _scafoldKey = GlobalKey<ScaffoldState>();
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> send() async {
// Platform messages may fail, so we use a try/catch PlatformException.
final MailOptions mailOptions = MailOptions(
body: 'Ro',
subject: 'the Email Subject',
recipients: ['rodrigo#houlak.com'],
isHTML: true,
attachments: [ 'path/to/image.png', ],
);
await FlutterMailer.send(mailOptions);
String platformResponse;
try {
await FlutterMailer.send(mailOptions);
platformResponse = 'success';
} catch (error) {
platformResponse = error.toString();
}
if (!mounted) return;
_scafoldKey.currentState.showSnackBar(SnackBar(
content: Text(platformResponse),
));
}
I had the same issue on iPhone, it was caused because I hadn't set up the default iOS default Mail App.
Adding in AndroidManifest.xml this for me on Android solve the issue:
<application .... />
// add queries tag for mailto intent out side of application tag
<queries>
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
</queries>
make sure the Android Gradle Plugin is higher then 4.1.0 you will find it inside android\build.gradle file some thing like this
dependencies { classpath 'com.android.tools.build:gradle:4.1.1' "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.4' }
2 . add this to your android/app/src/main/AndroidManifest.xml file just after where you add the <uses-permission>
<queries> <intent> <action android:name="android.intent.action.SENDTO" /> <data android:scheme="mailto" /> </intent> </queries>
run flutter clean
run flutter pub get or you can run your code it will do this for you after that Flutter email sender package should works for you
this solve the issue for me

How do I open a web browser (URL) from my Flutter code?

I am building a Flutter app, and I'd like to open a URL into a web browser or browser window (in response to a button tap). How can I do this?
TL;DR
This is now implemented as Plugin
const url = "https://flutter.io";
if (await canLaunchUrl(url))
await launchUrl(url);
else
// can't launch url, there is some error
throw "Could not launch $url";
Full example:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(new Scaffold(
body: new Center(
child: new RaisedButton(
onPressed: _launchURL,
child: new Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
final uri = Uri.parse(url);
if (await canLaunchUrl(uri)) {
await launchUrl(uri);
} else {
throw 'Could not launch $url';
}
}
In pubspec.yaml
dependencies:
url_launcher: ^6.1.7
Check out the latest url_launcher package.
Special Characters:
If the url value contains spaces or other values that are now allowed in URLs, use
Uri.encodeFull(urlString) or Uri.encodeComponent(urlString) and pass the resulting value instead.
If you target sdk 30 or above canLaunch will return false by default due to package visibility changes: https://developer.android.com/training/basics/intents/package-visibility
in the androidManifest.xml you'll need to add the following directly under <manifest>:
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
Then the following should word - for flutter 3 upwards:
const uri = Uri.parse("https://flutter.io");
if (await canLaunchUrl(uri)){
await launchUrl(uri);
} else {
// can't launch url
}
or for older versions of flutter use this instead:
const url = "https://flutter.io";
if (await canLaunch(url)){
await launch(url);
} else {
// can't launch url
}
launchUrl has a mode parameter which can be used to control where the url gets launched.
So, passing in launchUrl(uri, mode: LaunchMode.platformDefault) leaves the decision of how to launch the URL to the platform
implementation. But you can also specify
LaunchMode.inAppWebView which will use an in-app web view
LaunchMode.externalApplication for it to be handled by an external application
Or LaunchMode.externalNonBrowserApplication to be handled by a non-browser application.
For Flutter:
As described above by Günter Zöchbauer
For Flutter Web:
import 'dart:html' as html;
Then use:
html.window.open(url, name);
Make sure that you run flutter clean if the import doesn't resolve.
For those who wants to implement LAUNCH BROWSER AND EXIT APP by using url_launcher. Remember to use (forceSafariVC: false) to open the url in default browser of the phone. Otherwise, the launched browser exit along with your APP.
await launch(URL, forceSafariVC: false);
The best way is to use url_launcher package .
Add url_launcher as a dependency in your pubspec.yaml file.
dependencies:
url_launcher:
An example of how to use it :
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Flutter is beautiful'),),
body: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
)),
);
}
_launchURL() async {
const url = 'https://flutter.dev';
if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(Uri.parse(url));
} else {
throw 'Could not launch $url';
}
}
Output :
The launch method takes a string argument containing a URL .
By default, Android opens up a browser when handling URLs. You can
pass forceWebView: true parameter to tell the plugin to open a WebView
instead. If you do this for a URL of a page containing JavaScript,
make sure to pass in enableJavaScript: true, or else the launch method
will not work properly. On iOS, the default behavior is to open all
web URLs within the app. Everything else is redirected to the app
handler.
This is now implemented as Plugin
const url = "https://flutter.io";
final Uri _url = Uri.parse(url);
await launchUrl(_url,mode: LaunchMode.externalApplication);
pubspec.yaml
Add dependencies
dependencies: url_launcher: ^6.0.12
Output
If you want to use url_launcher than please use it in this form
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
url_launcher: ^5.0.2
flutter:
sdk: flutter
This answer is also for absolute beginners: They are thinking behind the flutter sdk.
No that was a failure. The packages were extras and not in the flutter Sdk. These were secondary packages (single small framework helpers).
The PLUGIN plugin works great, as you explain in your examples.
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
final Uri _url = Uri.parse('https://flutter.dev');
void main() => runApp(
const MaterialApp(
home: Material(
child: Center(
child: ElevatedButton(
onPressed: launchUrlStart(url: "https://flutter.dev"),
child: Text('Show Flutter homepage'),
),
),
),
),
);
Future<void> launchUrlStart({required String url}) async {
if (!await launchUrl(Uri.parse(url))) {
throw 'Could not launch $url';
}
}
But when trying to open PDF https://www.orimi.com/pdf-test.pdf it remained blank, the problem was that the browser handled it in its own way. Therefore the solution was to tell it to open with an external application and it worked as expected.
Future<void> launchUrlStart({required String url}) async {
if (!await launchUrl(Uri.parse(url),mode: LaunchMode.externalApplication)) {
throw 'Could not launch $url';
}
}
In pubspec.yaml
#https://pub.dev/packages/url_launcher
url_launcher: ^6.1.5
using the url_launcher package to do the following:
dependencies:
url_launcher: ^latest_version
if (await canLaunchUrl(Uri.parse(url))) {
await launchUrl(Uri.parse(url));
}
Note: Ensure you are trying to open a URI, not a String.