【问题标题】:how to get mobile number in flutter如何在flutter中获取手机号码
【发布时间】:2021-06-29 21:39:27
【问题描述】:

我正在使用“mobile_number(version - 1.0.3)”插件在颤振应用程序中获取手机号码,正在原始设备上运行,但我无法获取手机号码。我可以将手机号码设为空而不是错误其他 sim 卡详细信息如屏幕截图所示。

帮我解决这个问题,我刚刚复制粘贴了插件给出的示例代码 plugin link

【问题讨论】:

  • 我认为这个包有一些问题,因为它不适用于oppo手机。

标签: flutter flutter-layout flutter-dependencies dart-pub


【解决方案1】:

它说: 注意:如果手机号码不在 sim 卡上,则不会返回电话号码。

如果 SIM 卡不是原始的(即更换),我认为手机号码不会预先存在于 SIM 卡上

【讨论】:

  • 可以不用插件吗?
【解决方案2】:

如果电话号码未存储在 sim 卡上(又名 null),那么您无法从其他任何地方获取它,在这种情况下,您可能希望将用户转发到可以输入电话的其他页面使用 TextField 编号,然后将其存储在某处

【讨论】:

    【解决方案3】:

    使用Mobile_number包获取手机号码等详细信息。例如

    import 'dart:async';
    
    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:mobile_number/mobile_number.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String _mobileNumber = '';
      List<SimCard> _simCard = <SimCard>[];
    
      @override
      void initState() {
        super.initState();
        MobileNumber.listenPhonePermission((isPermissionGranted) {
          if (isPermissionGranted) {
            initMobileNumberState();
          } else {}
        });
    
        initMobileNumberState();
      }
    
      // Platform messages are asynchronous, so we initialize in an async method.
      Future<void> initMobileNumberState() async {
        if (!await MobileNumber.hasPhonePermission) {
          await MobileNumber.requestPhonePermission;
          return;
        }
        String mobileNumber = '';
        // Platform messages may fail, so we use a try/catch PlatformException.
        try {
          mobileNumber = (await MobileNumber.mobileNumber)!;
          _simCard = (await MobileNumber.getSimCards)!;
        } on PlatformException catch (e) {
          debugPrint("Failed to get mobile number because of '${e.message}'");
        }
    
        // If the widget was removed from the tree while the asynchronous platform
        // message was in flight, we want to discard the reply rather than calling
        // setState to update our non-existent appearance.
        if (!mounted) return;
    
        setState(() {
          _mobileNumber = mobileNumber;
        });
      }
    
      Widget fillCards() {
        List<Widget> widgets = _simCard
            .map((SimCard sim) => Text(
                'Sim Card Number: (${sim.countryPhonePrefix}) - ${sim.number}\nCarrier Name: ${sim.carrierName}\nCountry Iso: ${sim.countryIso}\nDisplay Name: ${sim.displayName}\nSim Slot Index: ${sim.slotIndex}\n\n'))
            .toList();
        return Column(children: widgets);
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Center(
              child: Column(
                children: <Widget>[
                  Text('Running on: $_mobileNumber\n'),
                  fillCards()
                ],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 2019-12-07
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 2014-04-16
      相关资源
      最近更新 更多