androidflutterdartsmsdual-sim

Get the list of user SIM cards and select it - flutter


For the flutter application, it is necessary to send an SMS with the user's SIM card, and for this purpose, I want it to be possible to select the desired SIM card in dual-SIM phones.

i check sms_plugin package but user can't select SIM cart you must select in code .


Solution

  • you can try https://pub.dev/packages/mobile_number this package,

    Add dependency

    dependencies:
      mobile_number: ^1.0.4
    

    Code snippet

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