В initState я добавил _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]);
});
}
Код: Выделить всё
@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);
});
}
}
Мой пример смс с хешем:
123456 — ваш OTP.
AB/C1defgH2
Но codeUpdated() блок не посещается. Поэтому я не могу прочитать otp...
Я также добавил разрешения, такие как получение SMS и чтение SMS, в мой файл Androidmanifest.
Как заставить это работать? Пожалуйста, помогите!
Подробнее здесь: https://stackoverflow.com/questions/788 ... rom-the-sm
Мобильная версия