From 40413c4fb8770482e72b752cd885d8ccc118015f Mon Sep 17 00:00:00 2001 From: "khoa.nguyen" Date: Wed, 13 Mar 2024 14:25:17 +0700 Subject: [PATCH] handle the scenario where strategies are empty --- lib/src/otp_text_edit_controller.dart | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/src/otp_text_edit_controller.dart b/lib/src/otp_text_edit_controller.dart index 3020dc6..0135647 100644 --- a/lib/src/otp_text_edit_controller.dart +++ b/lib/src/otp_text_edit_controller.dart @@ -76,6 +76,8 @@ class OTPTextEditController extends TextEditingController { if (strategiesListen != null) ...strategiesListen, ]; + if (list.isEmpty) return Future.value(); + return Stream.fromFutures(list).first.then( (value) { if (autoStop) { @@ -112,10 +114,14 @@ class OTPTextEditController extends TextEditingController { (e) => e.listenForCode(), ); - return Stream.fromFutures([ + final list = [ if (platform.isAndroid) smsListen, if (strategiesListen != null) ...strategiesListen, - ]).first.then( + ]; + + if (list.isEmpty) return Future.value(); + + return Stream.fromFutures(list).first.then( (value) { if (autoStop) { stopListen(); @@ -146,9 +152,14 @@ class OTPTextEditController extends TextEditingController { ExtractStringCallback codeExtractor, ) { final strategiesListen = strategies?.map((e) => e.listenForCode()); - Stream.fromFutures([ + + final list = [ if (strategiesListen != null) ...strategiesListen, - ]).first.then((value) { + ]; + + if (list.isEmpty) return; + + Stream.fromFutures(list).first.then((value) { text = codeExtractor(value); }); }