Hi, I’m using ViewModel to manage global states, i...
# multiplatform
p
Hi, I’m using ViewModel to manage global states, it works with Android but throws an exception with iOS.
Copy code
Uncaught Kotlin exception: kotlin.UnsupportedOperationException: `Factory.create(String, CreationExtras)` is not implemented. You may need to override the method and provide a custom implementation. Note that using `Factory.create(String)` is not supported and considered an error.
But got the same exception
p
I believe you have to use the overload that takes an
initialize {}
lambda
p
Copy code
fun MyComposable(viewModel: MyViewModel = viewModel { MyViewModel() })
like this right? I just changed to this and it is working now
This one didn’t work
Copy code
fun MyComposable(viewModel: MyViewModel = viewModel())
But I don’t really understand why it works 🙂
p
Far from the Mac right now but in general the
viewModel()
function doesn't know how to create your ViewModel unless you tell it how to, with the initializer parameter. You can use koin in the meantime. It has support for that already.
p
It’s strange for me that it works on Android, but not iOS
and it does not show any error in compile time
Let me check what Koin is, thanks @Pablichjenkov
👍 1
p
It should have a better error message right but is very fresh. In the docs you linked it explains in the bottom that, in JVM it is possible to instantiate a ViewModel class
that has no constructor parameters
using reflection. But outside JVM this is not possible. Koin is a DI library, it offers first class support for compose multiplatform, worth checking
👍 2
514 Views