import 'dart:async'; 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 { String _mobileNumber = ''; List _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 initMobileNumberState() async { if (!await MobileNumber.hasPhonePermission) { await MobileNumber.requestPhonePermission; return; } // 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(() {}); } Widget fillCards() { List 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: [ Text('Running on: $_mobileNumber\n'), fillCards() ], ), ), ), ); } }