I am getting an error and cannot figure out how to fix it. Everything looks correct to me. Please help!

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)
        }
    }
}

Could yo show ProfileView struct ?

Did you declare an there ? You should.

    @EnvironmentObject var userViewModel: UserViewModel

Here is the ProfileView struct:

import SwiftUI

struct ProfileView: View { @EnvironmentObject var user: UserViewModel

var body: some View { VStack { Text("Employee Name: \(user.user?.firstName ?? "") \(user.user?.lastName ?? "")") Text("Store Number: \(user.user?.storeNumber ?? 0)") Text("Team Member Number: \(user.user?.teamMemberNumber ?? 0)") } }

}

You declared

.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.

on the navigationLink. It seems to be the error. See: https://developer.apple.com/forums/thread/653367

You should only declare on

            NavigationSplitView {

as you did at the end of ContentView.

Updated ContentView struct:

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")
                    } 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)
        }
    }
}

Hard to say without seeing the complete code and be able to run.

But it could be that .environmentObject is applied to the wrong part.

Try this (see line 69):

1. struct ContentView: View {
2.     @EnvironmentObject var myData: MyData
3.     @EnvironmentObject var userViewModel: UserViewModel
4.     
5.     var body: some View {
6.         VStack {
7.             NavigationSplitView {
8.                 List {
9.                     NavigationLink {
10.                         MainApp()
11.                             .navigationTitle ("Counter")
12.                     } label: {
13.                         HStack {
14.                             Text("Counter")
15.                                 .font(.largeTitle)
16.                                 .foregroundColor(Color.red)
17.                             Spacer()
18.                             Image(systemName: "plus.forwardslash.minus")
19.                                 .font(.largeTitle)
20.                                 .foregroundColor(Color.red)
21.                                 .onTapGesture {
22.                                     myData.totalLeft = myData.total - myData.counter
23.                                 }
24.                         }
25.                     }
26.                     NavigationLink {
27.                         Settings()
28.                             .navigationTitle("Settings")
29.                     } label: {
30.                         HStack {
31.                             Text("Settings")
32.                                 .font(.largeTitle)
33.                                 .foregroundColor(Color.red)
34.                             Spacer()
35.                             Image(systemName: "gear")
36.                                 .font(.largeTitle)
37.                                 .foregroundColor(Color.red)
38.                         }
39.                     }
40.                     NavigationLink {
41.                         Metrics()
42.                             .navigationTitle("Metrics")
43.                     } label: {
44.                         HStack {
45.                             Text("Metrics")
46.                                 .font(.largeTitle)
47.                                 .foregroundColor(Color.red)
48.                             Spacer()
49.                             Image(systemName: "chart.bar")
50.                                 .font(.largeTitle)
51.                                 .foregroundColor(Color.red)
52.                         }
53.                     }
54.                     NavigationLink {
55.                         ProfileView()
56.                             .navigationTitle ("Account")
57.                     } label: {
58.                         HStack {
59.                             Text("Account")
60.                                 .font(.largeTitle)
61.                                 .foregroundColor(Color.red)
62.                             Spacer()
63.                             Image(systemName: "person")
64.                                 .font(.largeTitle)
65.                                 .foregroundColor(Color.red)
66.                         }
67.                     }
68.                 }
69.                 .environmentObject(userViewModel)
70.             } detail: {
71.                 Text("Select a Page")
72.             }
73.         }
74.     }
75. }

I am getting an error and cannot figure out how to fix it. Everything looks correct to me. Please help!
 
 
Q