The line with the comment below has the error and says the error I am receiving. If you need to see anything else, do not hesitate to ask. I'd be happy to provide it.
struct ContentView: View {
@EnvironmentObject var myData: MyData
@EnvironmentObject var userViewModel: UserViewModel
var body: some View {
VStack {
NavigationSplitView {
List {
NavigationLink {
MainApp()
.navigationTitle ("Counter")
} label: {
HStack {
Text("Counter")
.font(.largeTitle)
.foregroundColor(Color.red)
Spacer()
Image(systemName: "plus.forwardslash.minus")
.font(.largeTitle)
.foregroundColor(Color.red)
.onTapGesture {
myData.totalLeft = myData.total - myData.counter
}
}
}
NavigationLink {
Settings()
.navigationTitle("Settings")
} label: {
HStack {
Text("Settings")
.font(.largeTitle)
.foregroundColor(Color.red)
Spacer()
Image(systemName: "gear")
.font(.largeTitle)
.foregroundColor(Color.red)
}
}
NavigationLink {
Metrics()
.navigationTitle("Metrics")
} label: {
HStack {
Text("Metrics")
.font(.largeTitle)
.foregroundColor(Color.red)
Spacer()
Image(systemName: "chart.bar")
.font(.largeTitle)
.foregroundColor(Color.red)
}
}
NavigationLink {
ProfileView()
.navigationTitle ("Account")
.environmentObject(userViewModel) //Thread 1: Fatal error: No ObservableObject of type UserViewModel found. A View.environmentObject(_:) for UserViewModel may be missing as an ancestor of this view.
} label: {
HStack {
Text("Account")
.font(.largeTitle)
.foregroundColor(Color.red)
Spacer()
Image(systemName: "person")
.font(.largeTitle)
.foregroundColor(Color.red)
}
}
}
} detail: {
Text("Select a Page")
}
}
.environmentObject(userViewModel)
}
}
import Foundation
import FirebaseAuth
import FirebaseFirestore
import FirebaseFirestoreSwift
class UserViewModel: ObservableObject {
@Published var user: User?
private let auth = Auth.auth()
private let db = Firestore.firestore()
var uuid: String? {
auth.currentUser?.uid
}
var userIsAuthenticated: Bool {
auth.currentUser != nil
}
var userIsAuthenticatedAndSynced: Bool {
user != nil && userIsAuthenticated
}
private func sync() {
guard userIsAuthenticated else { return }
let docRef = db.collection("users").document(self.uuid!)
docRef.getDocument { (document, error) in
guard let document = document, document.exists, error == nil else {
print("Error retrieving document: \(error!)")
return
}
do {
let data = document.data()
let jsonData = try JSONSerialization.data(withJSONObject: data as Any, options: .prettyPrinted)
let user = try JSONDecoder().decode(User.self, from: jsonData)
self.user = user
} catch {
print("Sync error: \(error)")
}
}
}
private func add (_ user: User) {
guard userIsAuthenticated else { return }
do {
let userData = try JSONSerialization.jsonObject(with: try JSONEncoder().encode(user), options: []) as? [String: Any]
let _ = try db.collection("users").document(self.uuid!).setData(userData ?? [:])
} catch {
print("Error adding: \(error)")
}
}
private func update() {
guard userIsAuthenticatedAndSynced else { return }
do {
let userData = try JSONSerialization.jsonObject(with: try JSONEncoder().encode(user), options: []) as? [String: Any]
let _ = try db.collection("users").document(self.uuid!).setData(userData ?? [:])
} catch {
print("Error updating: \(error)")
}
}
}
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject var userViewModel = UserViewModel()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(MyData())
.environmentObject(userViewModel)
}
}
}