init method
Call this once to initialize the plugin and start listening to OTPs.
Implementation
Future<void> init() async {
if (_isInitialized) return;
_isInitialized = true;
// Handle native messages
_channel.setMethodCallHandler((call) async {
if (call.method == "onOtpReceived") {
final otp = call.arguments as String?;
if (otp != null && !_otpStreamController.isClosed) {
_otpStreamController.add(otp);
}
}
});
// Start SMS listener on Android side
try {
await _channel.invokeMethod('startListening');
} catch (e, stackTrace) {
debugPrint("Error starting SMS retriever: $e");
debugPrintStack(stackTrace: stackTrace);
}
}