Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter get the phone number

Tags:

flutter

dart

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

like image 632
GreenTigerEye Avatar asked Jan 04 '18 07:01

GreenTigerEye


People also ask

How do I use contact picker in Flutter?

flutter_contact_picker. With this plugin a Flutter app can ask its user to select a contact from his/her address book. The information associated with the contact is returned to the app. This plugin uses the operating system's native UI for selecting contacts and does not require any special permissions from the user.


3 Answers

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;
like image 96
sarin upreti Avatar answered Sep 18 '22 12:09

sarin upreti


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}');
    }
  }
like image 43
Rodion Mostovoy Avatar answered Sep 16 '22 12:09

Rodion Mostovoy


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.

like image 44
Rene Avatar answered Sep 20 '22 12:09

Rene