https://kotlinlang.org logo
#koin
Title
# koin
m

Merseyside

10/06/2023, 12:09 PM
Hi! Am I allowed to write something like that:
Copy code
scoped {
    val accountId: Id? = getOrNull<Id>()
    if (accountId != null) {
        GetProfileFlowInteractor(get(), get(), accountId)
    } else null
}
If i'm not, how can I provide this accountId and use it to avoid declaring this definition at all?
p

Pedro Francisco de Sousa Neto

10/06/2023, 12:52 PM
Yes, you can write this code. But I think it can be improved to something like this:
Copy code
scoped<GetProfileFlowInteractor> { (Id?: accountId) ->
    GetProfileFlowInteractorImpl(
        abc = get(),
        otherThing = get(),
        accountId = accountId,
    )
}
And you can retrieve this like: 123 is your accountId parameter
Copy code
val profileFlow = get<GetProfileFlowInteractor> { parametersOf(123) }
Of course, you need to handle nullability behaviours of accountId inside
GetProfileFlowInteractorImpl
.
I suggest this way to cover this “null behaviour” with unit test on
GetProfileFlowInteractor
. You will need create some logic like…
profileFlow.hasAccountId()
or
profileFlow.hasValidId()
.
m

Merseyside

10/06/2023, 1:37 PM
The trick is not to create this interactor at all by a condition or return null. For now I get this error when I provide null: IllegalStateException: Scoped instance not found for [someS*copeId].*
p

Pedro Francisco de Sousa Neto

10/06/2023, 2:53 PM
I never tried to use some dependency nullable, but I’m guessing to use like
scoped<GetProfileFlowInteractor?>
. Can you test this?
a

arnaud.giuliani

10/06/2023, 3:22 PM
there is no nullable in DSL type. But when you are requesting it you can use
getOrNull
or
injectOrNull
if you want
K 1
@Merseyside do you need a scope here?
m

Merseyside

10/06/2023, 3:35 PM
I use getOrNull and get this error. And yes I need scopes here
👍 1
a

arnaud.giuliani

10/06/2023, 4:18 PM
Copy code
val accountId: Id? = getOrNull<Id>()
is it an argument? or part of your scope
m

Merseyside

10/07/2023, 9:43 AM
@arnaud.giuliani I'm passing it in source arg when creating new scope
a

arnaud.giuliani

10/09/2023, 12:02 PM
you need to use declare on scope:
scope.declare(scopeId)
m

Merseyside

10/10/2023, 10:41 AM
Ok, I will try that! tnx:)
👍 2