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.
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.
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; 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.
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'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.