https://kotlinlang.org logo
Title
j

Javier

02/11/2023, 9:34 PM
I am getting a crash if I try to access to a data source in a similar approach to this
MoleculeViewModel
sample.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "....DataSource.getFlow()" because "this.datasource" is null
class PupperPicsViewModel(private val datasource: Datasource) : MoleculeViewModel<Event, Model>() {
  @Composable
  override fun models(events: Flow<Event>): Model {
    val data by datasource.flow.collectAsState()
    return PupperPicsPresenter(events, PupperPicsService())
  }
}
k

kevin.cianfarini

02/11/2023, 10:03 PM
remember
the call to datasource.flow?
j

Javier

02/11/2023, 10:28 PM
Composable calls are not allowed inside the calculation parameter of inline fun <T> remember(calculation: () -> TypeVariable(T)): TypeVariable(T)
Internally it is already using remember
@Composable
fun <T> produceState(
    initialValue: T,
    key1: Any?,
    key2: Any?,
    @BuilderInference producer: suspend ProduceStateScope<T>.() -> Unit
): State<T> {
    val result = remember { mutableStateOf(initialValue) }
    LaunchedEffect(key1, key2) {
        ProduceStateScopeImpl(result, coroutineContext).producer()
    }
    return result
}
remember { datasource }
fails too, with the null crash
anyway this doesn't look to be a molecule issue, it is a compose issue
h

hfhbd

02/11/2023, 11:01 PM
But how do you get the database, DI?
j

Javier

02/11/2023, 11:09 PM
Calling manually the constructor, I am just playing a bit with it
image.png
FooStore
=
PupperPicsViewModel
in the previous code