I am trying to integrate a flutter library sms_autofill. I have added the code as it was suggested.
In initState I added _listenCode();
@override
void initState() {
super.initState();
_initializeTextControllers();
_listenCode();
}
void _listenCode() async{
await SmsAutoFill().listenForCode();
}
void _initializeTextControllers() {
for (int i = 0; i < _controllers.length; i++) {
_controllers[i].addListener(() => _onTextChanged(i));
}
WidgetsBinding.instance.addPostFrameCallback((_) {
FocusScope.of(context).requestFocus(_focusNodes[0]);
});
}
And we have method named @override void codeUpdated()
which is expected to visit to read otp from sms which is as:
@override
void codeUpdated() {
// Handle auto-filled code
print("codeUpdated Received OTP visited "); // Debug log
String code = convertStreamToString(SmsAutoFill().code) as String;
_otp = code;
print("Received OTP: $code"); // Debug log
if (code.length == _controllers.length) {
setState(() {
for (int i = 0; i < _controllers.length; i++) {
_controllers[i].text = code[i];
}
// Move focus to the next field if needed
FocusScope.of(context).requestFocus(_focusNodes.last);
});
}
}
above all codes are written under block class _OtpScreenState extends State<OtpScreen> with CodeAutoFill {...}
My sample sms with hash is as :
<#>123456 is your OTP.
AB/C1defgH2
But codeUpdated()
block not get visited. So I am unable to read the otp…
I have added permissions like receive sms and read sms in my Androidmanifest file
too.
How to get it work? Please Help!