如何在颤振中获取手机号码

时间:2021-04-03 08:26:31

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

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

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

3 个答案:

答案 0 :(得分:0)

它说: 注意:如果手机号码不是预先存在于SIM卡上,则不会返回电话号码。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用 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()
            ],
          ),
        ),
      ),
    );
  }
}