'how to resolve lateinit property chargeSessionRepo has not been initialized

in a class which extends BroadcasrReceiver, I want to access a repository function. I get an error lateinit property chargeSessionRepo has not been initialized.

I have specified @Inject, what else needs to be done, so I can access the function in ChargeSessionRepository

class CustomNotificationListener : BroadcastReceiver() {
        @Inject
        lateinit var chargeSessionRepo: ChargeSessionRepository

        override fun onReceive(context: Context, intent: Intent) {
            Log.d("fcmService", "onReceive ${intent.getStringExtra("onClickListener")}")
            when(intent.getStringExtra("onClickListener")) {
                "approve" -> {
                    approvePlugin(true)
                }
                "deny" -> {
                    approvePlugin(false)
                }
            }
        }

        private fun approvePlugin(isApproved: Boolean) {
            GlobalScope.launch {
                try {
                    val chargeStatusUpdate = chargeSessionRepo.getChargeStatusUpdate().data
                    val chargeStatus = chargeStatusUpdate?.firstOrNull()

                    chargeStatus?.sessionId?.let {
                        chargeSessionRepo.approvePlugIn(it, isApproved)
                    }
                } catch (exception: Exception) {
                    Log.d("fcmService", "exception: ${exception}") // lateinit property chargeSessionRepo has not been initialized
                }
            }
        }
    }

ChargesessionRepository

@Singleton
class ChargeSessionRepository @Inject constructor(
    private val chargeSessionService: ChargeSessionService,
    private val chargeSessionDao: ChargeSessionDao

) : Repository() {

    var lastChargeStatus: List<ChargeStatusDTO>? = null
        private set

    suspend fun getChargeStatusUpdate(): Resource<List<ChargeStatusDTO>> {
        val resource = getResource { chargeSessionService.getChargeStatus() }
        lastChargeStatus = resource.data

        return resource
    }
}

could you please suggest how to do this

Thanks R



Solution 1:[1]

You may have forgotten to put @AndroidEntryPointon top of your broadcast receiver. if you are using hilt.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1