AlwaysDeveloper
AlwaysDeveloper

Reputation: 25

Know Source of Error - Catch Unhandled Exception Caused during Receive SMS Flutter S23

Getting error message during OTP auto fill process

Environment is: Flutter Release Build Flutter framework version 3.16.5 Mobilde device: Samsung Galaxy s23 ultra and S23. On other devices OTP reading and filling in OTP page is working fine.

Debugging using VSCode

Error received: There was a problem connecting. Try again later.

Issue is specific to this phone only so we want to catch the exception to know which package and why below error is coming:

Below is implementation. Please advise how we can know the source of exception. When error is recceived (as per picture) control is not passed in below funtion. But when we remove below implemention error stops coming .

So here are our thoughts:

  1. Either we need to implement some different method of boradcastreceiver which is even before onreceive
  2. Add any platform level exception handler which catches both flutter and android level excpetion and gives us stracktrace and can print in in mobile device / VS code console

Broadcast receiver class

class MySMSBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        try {
            if (intent != null && SmsRetriever.SMS_RETRIEVED_ACTION == intent.action && intent.extras != null ) {
                val extras = intent.extras
                val status : Status? = extras?.get(SmsRetriever.EXTRA_STATUS) as Status?
                unregister(receiver)
                if (status != null) {
                    when (status.statusCode) {
                        CommonStatusCodes.SUCCESS -> {
                            sms = extras?.getString(SmsRetriever.EXTRA_SMS_MESSAGE) as String
                            ignoreIllegalState {
                                val regex = Regex("\\b\\d{4,6}\\b")
                                val matches = regex.findAll(sms!!)
                                val result = matches.map { it.value }.toList()
                                pendingResult?.success(result[0])
                            }

                        }
                        CommonStatusCodes.TIMEOUT -> {
                            Log.e(Tag, "onReceive Timeout ")
                        }

                    }
                }
            }
        } catch (e : Exception) {
            Log.e(Tag, "onReceive Exception ${e}")
        }
    }
}

enter image description here

Upvotes: 0

Views: 29

Answers (0)

Related Questions