Hi! Am I allowed to write something like that: ```...
# koin
m
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
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
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
I never tried to use some dependency nullable, but I’m guessing to use like
scoped<GetProfileFlowInteractor?>
. Can you test this?
a
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
I use getOrNull and get this error. And yes I need scopes here
👍 1
a
Copy code
val accountId: Id? = getOrNull<Id>()
is it an argument? or part of your scope
m
@arnaud.giuliani I'm passing it in source arg when creating new scope
a
you need to use declare on scope:
scope.declare(scopeId)
m
Ok, I will try that! tnx:)
👍 2