Home > Back-end >  How to detect if otp is autofilled in flutter or not?
How to detect if otp is autofilled in flutter or not?

Time:08-18

In Flutter Application, I have otp screen on which there's a login button. After the otp is automatically filled, user clicks login and a loader appears and user is logged in. But what if user clicks login before the otp is filled? I want to handle this situation. What should i do in this?

CodePudding user response:

Most probably you should be having a controller for the otp field. On button click check if the otp controller is empty or incomplete. If not then do further execution. Else sure message otp is incomplete.

onTap :(){
  if(otpController.text.length == 4)
  {
   //Process further
  }
  else{
   //Otp isn't right
 }
}

CodePudding user response:

You can use Try - catch to check if credentials are correct.

try {
            PhoneAuthCredential credential = PhoneAuthProvider.credential(
              verificationId: verificationId,
              smsCode: codeController.text.trim(),
            );


            UserCredential userCredential = await 
          _auth.signInWithCredential(credential);
            
          }on FirebaseAuthException catch (e){
            Print(e.toString());


          }

CodePudding user response:

if you using firebase, if otp automatically filled, that make your app authenticated. user don't need to press that button at all,your app listen to authStateChanges() and should update ui, and i assume your entire app is listen to auth state. if not, try to management that auth state in your app, using streambuilder, changenotifier, bloc management,etc. your choice.

if otp isn't there let say the user has 2 phone, and otp go in on another phone, then user should fill it manually.

CodePudding user response:

Most probably you should be having a controller for the otp field. On button click check if the otp controller is empty or incomplete. If not then do further execution. Else sure message otp is incomplete.

onTap :(){
  if(otpController.text.isNotEmpty || otpController.text != "")
  {
   //Process further
  }
  else{
   //Otp isn't right
 }
}
  • Related