When trying to display all my accounts in my database by pressing a button in LeaderboardScreen
, I get this error while running the program. I don't know why as I'm sure that I have correctly implemented an @EnviromentObject
. Here is how my ContentView
looks like:
import SwiftUI
struct ContentView: View {
@State var isLoggedIn = false
@StateObject var accountListVM = AccountListViewModel()
@StateObject var loginVM = LoginViewModel()
@ObservedObject var userViewModel = UserViewModel()
var body: some View {
NavigationView{
LoginScreen()
.environmentObject(accountListVM)
.environmentObject(loginVM)
}
}
}
And this is my LeaderboardScreen
, where I get my error:
import SwiftUI
struct LeaderboardScreen: View {
@EnvironmentObject var accountlistVM: AccountListViewModel
@EnvironmentObject var loginVM: LoginViewModel
var body: some View {
VStack{
Button("See leaderboard") {
accountlistVM.getAllAccounts()
}
.padding()
if loginVM.isAuthenticated && accountlistVM.accounts.count > 0 {
List(accountlistVM.accounts, id: \.id) { account in
HStack{
Text("\(account.name)")
Text("Total counts: \(account.cookiesCount)")
}
}
.listStyle(PlainListStyle())
} else {
Text("Login to see your account")
}
}
}
}
To be more specific, here is where the error is located:
if loginVM.isAuthenticated && accountlistVM.accounts.count > 0 {