jaumard / sms_autofill

Flutter plugin to provide SMS code autofill support
MIT License
282 stars 174 forks source link

Not getting the second message #123

Open slorop41314 opened 2 years ago

slorop41314 commented 2 years ago

First otp sent and the listener are triggered,

when i press send again,the listener are not triggered, is it normal/intended case? or something wrong at my end?

Thanks.

Shayan-Aslani commented 2 years ago

First otp sent and the listener are triggered,

when i press send again,the listener are not triggered, is it normal/intended case? or something wrong at my end?

Thanks.

You must call SmsAutoFill().listenForCode again to receive second code.

pbs009 commented 2 years ago

What listener, I am not able to read the code. Can you provide some sample code? I tried copy pasting the example from pub.dev but that is not working.

millie-molly commented 2 years ago

hey I have same problem. Did you fix this?

pbs009 commented 2 years ago

hey I have same problem. Did you fix this?

Yes it working for me, can you explain me the scenario so that i can help you?

millie-molly commented 2 years ago

hey I have same problem. Did you fix this?

Yes it working for me, can you explain me the scenario so that i can help you?

hey I have same problem. Did you fix this?

Yes it working for me, can you explain me the scenario so that i can help you?

https://github.com/jaumard/sms_autofill/issues/145#issue-1048032097

here is my issue. I can get a new code but I have to keyboard down and open keyboard

I don't want this unnecessary step

hinaabbaskhan commented 1 year ago

Follow the following steps for resend OTP:

  1. Extend the class with CodeAutoFill in which you want to resend the OTP as done below

    class VerifyOTPScreen extends StatefulWidget {
    const VerifyOTPScreen({Key? key}) : super(key: key);
    
    @override
    State<VerifyOTPScreen> createState() => _VerifyOTPScreenState();
    }
class _VerifyOTPScreenState extends State<VerifyOTPScreen> with CodeAutoFill {

}
  1. Override the codeUpdated method and build the UI again when this method is called

    @override
    void codeUpdated() {
    print("Update code $code");
    setState(() {
      print("codeUpdated");
    });
    }
  2. Implement a listenOtp method which should be called at resend. In that method you need to unregister the previous listener and call listenForCode method before calling SmsAutoFill().listenForCode.
    Here is the method:

    void listenOtp() async {
     await SmsAutoFill().unregisterListener();
     listenForCode();
    await SmsAutoFill().listenForCode;
    print("OTP listen Called");
    }

You can see the whole sample code here:

import 'package:flutter/material.dart';
import 'package:sms_autofill/sms_autofill.dart';

class VerifyOTPScreen extends StatefulWidget {
  const VerifyOTPScreen({Key? key}) : super(key: key);

  @override
  State<VerifyOTPScreen> createState() => _VerifyOTPScreenState();
}

class _VerifyOTPScreenState extends State<VerifyOTPScreen> with CodeAutoFill {
  String codeValue = "";

  @override
  void codeUpdated() {
    print("Update code $code");
    setState(() {
      print("codeUpdated");
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    listenOtp();
  }

  void listenOtp() async {
    await SmsAutoFill().unregisterListener();
    listenForCode();
    await SmsAutoFill().listenForCode;
    print("OTP listen Called");
  }

  @override
  void dispose() {
    SmsAutoFill().unregisterListener();
    print("unregisterListener");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: const EdgeInsets.all(30.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Center(
              child: PinFieldAutoFill(
                currentCode: codeValue,
                codeLength: 4,
                onCodeChanged: (code) {
                  print("onCodeChanged $code");
                  setState(() {
                    codeValue = code.toString();
                  });
                },
                onCodeSubmitted: (val) {
                  print("onCodeSubmitted $val");
                },
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
                onPressed: () {
                  print(codeValue);
                },
                child: const Text("Verify OTP")),
            ElevatedButton(onPressed: listenOtp, child: const Text("Resend"))
          ],
        ),
      ),
    );
  }
}