I've got a view that uses the following view modifier:
.fullScreenCover(isPresented: $viewModel.user) {
This will generate a error:
Cannot convert value of type 'Binding<User?>' to expected argument type 'Binding<Bool>'
When the user taps the login button, it will trigger a call to an async method in the viewModel that will set its user property if the login was successful or set its error property if not.
ViewModel:
class LoginViewModel: ObservableObject {
@Published var user:User?
@Published var error:Error?
...
}
I know I can create a couple of bool published vars, but I would like to avoid having to places where I acknowledge a user has logged successfully ( with the User object and a bool )
Thanks
Using the
fullScreenCover(item:onDismiss:content:)method should do what you want it to.Generally I would recommend making an
enumlike this though:The benefit is that this will prevent you from getting into a bad state where both
useranderrorare present.Adding a computed boolean variable that returns whether
userStateis currently.userwould allow you to use your currentfullScreenCoverinitialization without adding a second source of truth.As a side note, you could also use a tool like PointFree's SwiftUI Navigation to use the enum value directly for presenting the sheet: