This is a FLutter Plugin to get the device mobile number.
https://pub.dev/packages/mobile_number
if you still using depecated FlutterActivty on MainActivity.java which is import of
import io.flutter.app.FlutterActivity;
not
import io.flutter.embedding.android.FlutterActivity;
then you need to add the following to your MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MobileNumberPlugin.registerWith(registrarFor("com.amorenew.mobile_number.MobileNumberPlugin()"));
}
await MobileNumber.hasPhonePermission
await MobileNumber.requestPhonePermission
if (isPermissionGranted) {
//Get mobile number
} else {
//Request Phone Permission
}
});
permission_handler has a better implementation of device general permissions.
A simple example would be:
var status = await Permission.phone.status;
if (!status.isGranted) {
status = await Permission.phone.request();
}
if (!status.isGranted) {
// return or throw some error
}
return await MobileNumber.mobileNumber;
final String mobileNumber = await MobileNumber.mobileNumber;
return mobileNumber;
}
final List<SimCard> simCards = await MobileNumber.getSimCards;
return simCards;
}