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
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.
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;
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}');
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With