I have used the sms_autofill plugin to to auto-detect the OTP received on mobile and type it in the given PinFieldAutoFill, As per https://pub.dev/packages/sms_autofill#-readme-tab- I am using await SmsAutoFill().listenForCode; function before making my API request & when the request has got a 200 status code I'm navigating it to another page and in init I'm listening to the OTP SMS received by using await SmsAutoFill().listenForCode; I thought like maybe when the page is initialized it wont be listening again to any SMS so i did add a Timer also but still it was not receiving anything.
Dependency - sms_autofill: ^1.2.6
Code : Page1
Center(
child: RaisedButton(
child: Text('Register'),
onPressed: () async {
await SmsAutoFill().listenForCode;
final signCode = await SmsAutoFill().getAppSignature;
print(signCode);
//Http request code
},
),
),
Code : Page2
String appSignature;
String otpCode;
Timer timer;
@override
void codeUpdated() {
setState(() {
otpCode = code;
});
}
@override
void initState() {
super.initState();
listenOTP();
// timer = Timer.periodic(Duration(seconds: 10), (Timer t){
// print('OTP Listening');
// listenOTP();
// });
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Code Received: $otpCode",
),
Container(
padding: EdgeInsets.symmetric(horizontal: 50),
child: PinFieldAutoFill(
codeLength: 6,
onCodeChanged: (val) {
print(val);
},
),
)
],
),
),
);
}
void listenOTP() async {
print('listen for code');
await SmsAutoFill().listenForCode;
}