I am getting a crash if I try to access to a data ...
# squarelibraries
j
I am getting a crash if I try to access to a data source in a similar approach to this
MoleculeViewModel
sample.
Copy code
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "....DataSource.getFlow()" because "this.datasource" is null
Copy code
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
remember
the call to datasource.flow?
j
Copy code
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
Copy code
@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
But how do you get the database, DI?
j
Calling manually the constructor, I am just playing a bit with it
image.png
FooStore
=
PupperPicsViewModel
in the previous code