13

How can I get the mobile phone number of the current phone where the application is running?

For example for this plugin? It would be cool to get the mobile phone number directly.

https://github.com/flutter/plugins/pull/329

4 Answers 4

18

this library in dart pub seem to do what you are looking for. check the package linked below:

https://pub.dev/packages/sms_autofill

PhoneFieldHint [Android only]

final SmsAutoFill _autoFill = SmsAutoFill(); final completePhoneNumber = await _autoFill.hint; 
Sign up to request clarification or add additional context in comments.

4 Comments

Yep exactly what I have searched for with PhoneFieldHint.
Great answer but I think the OP was asking about how to get a mobile number. Still this is super useful.
this is not working in release mode.
When I implemented this, the pop for phone number is coming and going away instantly any idea why..?
9

There is no way to do this with native Android/iOS code, so also not in Flutter. For example WhatsApp just ask the user to type their number and then sends an SMS with a verification code.

Edit: It is indeed now possible to ask for a phonenumber which makes it easier for the user on Android: https://pub.dev/packages/sms_autofill

However, these are phone numbers known by Google and not necessarily the phone number that is of the phone itself. Also, the user still has to select one themselves and you can't just get the info from the device.

2 Comments

There's a package in flutter now, for doing this. Also, its possible in Android, as Swiggy does it. The pakage I am refering to is: mobile_number
True, when I wrote this comment it was only just possible on Android and I didn't know about it yet. (It was launched in oktober 2017.) I'll edit the answer.
9

Important: Note that mobile_number plugin asks a very special permission to make phone calls.
sms_autofill do not ask any permissions at all. Use it like this:

 @override void initState() { super.initState(); Future<void>.delayed(const Duration(milliseconds: 300), _tryPasteCurrentPhone); } Future _tryPasteCurrentPhone() async { if (!mounted) return; try { final autoFill = SmsAutoFill(); final phone = await autoFill.hint; if (phone == null) return; if (!mounted) return; _textController.value = phone; } on PlatformException catch (e) { print('Failed to get mobile number because of: ${e.message}'); } } 

3 Comments

Hi, I would like to ask, I have tried your code and finally getting pop up of user's phone number. is it possible to only print the phone number without showing the pop up?
I don't think so. This feature is a part of OS (or some of Google services), so a user have to choose a phone number by himself.
When I implemented this, the pop for phone number is coming and going away instantly any idea why..?
1

There's another package now mobile_number

Its as simple as

mobileNumber = (await MobileNumber.mobileNumber) 

Note: It works for Android only, because getting the mobile number of a sim card is not supported in iOS.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.