'I want to catch or read OTP of SMS messages automatically. I did some tests like this code : using altauto_fill sms

 Future<void> initSmsListener() async {
    String? comingSms;
    try {
      comingSms = await AltSmsAutofill().listenForSms;
    } on PlatformException {
      comingSms = 'Failed to get Sms.';
    }

    print("====>Message: ${comingSms}");
    print(
        "${comingSms![0]}${comingSms[1]}${comingSms[2]}${comingSms[3]}${comingSms[4]}${comingSms[5]}");
    otpCode =
        '${comingSms[0]}${comingSms[1]}${comingSms[2]}${comingSms[3]}${comingSms[4]}${comingSms[5]}';
    otp.text = otpCode;
    // textEditingController1.text = _comingSms[32] + _comingSms[33] + _comingSms[34] + _comingSms[35]
    //     + _comingSms[36] + _comingSms[37]; //used to set the code in the message to a string and setting it to a textcontroller. message length is 38. so my code is in string index 32-37.
  }
@override
  void onInit() {
    initSmsListener();
    super.onInit();
  }

Flutter documentation for oneTimeCode: https://api.flutter.dev/flutter/services/AutofillHints/oneTimeCode-constant.html

Flutter Autofill : https://github.com/flutter/flutter/blob/7891006299/packages/flutter/lib/src/services/autofill.dart#L362

  • Ask for Permission
  • Auto read if permission is allowed
  • verify the otp with server


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source