Flutter获取电话号码

时间:2018-01-04 07:48:20

标签: dart flutter

如何获取运行应用程序的当前手机的手机号码?

例如这个插件?直接获取手机号码会很酷。

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

4 个答案:

答案 0 :(得分:5)

使用原生Android / iOS代码无法做到这一点,因此Flutter也无法做到这一点。例如,WhatsApp只是要求用户输入他们的号码,然后发送带有验证码的短信。

答案 1 :(得分:1)

dart pub中的这个库似乎可以满足您的需求。检查下面链接的软件包:

https://pub.dev/packages/sms_autofill

PhoneFieldHint [仅适用于Android]

unknown file:0: error: Failure in TEST(
     NOTE: Assertion happened without being in a test run (perhaps in main?), 
           Something is very wrong. Check this assertion and fix)

           Something is very wrong. Check this assertion and fix:0: error:
    Deallocating non-allocated memory
   allocated at file: <unknown> line: 0 size: 0 type: unknown
   deallocated at file: <unknown> line: 0 type: delete

答案 2 :(得分:0)

现在有另一个包裹mobile_number

就这么简单

mobileNumber = (await MobileNumber.mobileNumber)

注意:仅适用于 Android,因为 iOS 不支持获取 SIM 卡的手机号码。

答案 3 :(得分:0)

重要提示:请注意,mobile_number 插件需要非常特殊的权限才能拨打电话。
sms_autofill 不要询问任何权限。 像这样使用它:

  @override
  void initState() {
    super.initState();
    Future<void>.delayed(const Duration(milliseconds: 300), _tryPasteCurrentPhone);
  }

  Future _tryPasteCurrentPhone() async {
    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}');
    }
  }