hread 1: Fatal error: No ObservableObject of type Wallet found.

I have this wallet object that I use to do a lot in my app. I want to have a navigation from a login screen to go to here but every time it runs I get the tread 1: fatal error: No observableObject of type wallet found. I have an environmental object to the class call of type Wallet()


        VStack{

            headerView

            ScrollView(.horizontal, showsIndicators: false){

                HStack(spacing: 10){

                    ForEach(self.wallet.cards.indices, id:\

                    .self) { index in

                        CardView(card:

                                    self.wallet.cards[index])

                        .onTapGesture {

                            self.wallet.cards.indices.forEach{ index in

                                self.wallet.cards[index].isSelected = false

                            }

                            self.wallet.cards[index].isSelected.toggle()

                        }

                    }

                }

            }

            Spacer()

        }

Did you connect wallet to the environment ?

        VStack {
           // Code
        }
        .onAppear() {
          // some code if needed, as var initialisations
        }
        .environmentObject(wallet)        // to connect to value in environment

You need to take care with evironmentObject. If the connection to environment is missing, that causes an execution error, no compilation error.

Here is all of the code that I have in this file. I have an environmentObject for Wallet() and also added on for wallet but the Thread 1: Fatal error: No ObservableObject of type Wallet found error still shows up. Any help would be appreciated


//  CardListView.swift

//  idApp

//

//  

//



import SwiftUI



struct CardListView: View {

    @StateObject private var viewModel = ViewModel()

    @EnvironmentObject var wallet: Wallet

    

    var headerView: some View{

        HStack{

            Text("Healthcare cards")

                .font(.title2)

                .foregroundColor(Color(.black))

                .fontWeight(.bold)

            Spacer()

            Button("Add new card") {}

                .font(.callout)

                .foregroundColor(Color.primaryBlue)

                .padding(.trailing)

        }

    }

    

    var body: some View {

        VStack{

            headerView

            ScrollView(.horizontal, showsIndicators: false){

                HStack(spacing: 10){

                    ForEach(self.wallet.cards.indices, id:\

                    .self) { index in

                        CardView(card:

                                    self.wallet.cards[index])

                        .onTapGesture {

                            self.wallet.cards.indices.forEach{ index in

                                self.wallet.cards[index].isSelected = false

                            }

                            self.wallet.cards[index].isSelected.toggle()

                        }

                    }

                }

            }

            Spacer()

        }

        .environmentObject(wallet)

    }

}



struct CardListView_Previews: PreviewProvider {

    static var previews: some View {

        CardListView()

            .environmentObject(Wallet())

    }

}

Where do you declare a StateObject:

    @StateObject var wallet = Wallet() 

That could be in the mainApp:

@main
struct TheApp: App {
    
    @StateObject var wallet = Wallet() 

    var body: some Scene {
        WindowGroup {
            CardListView()
                .environmentObject(wallet)         // Essential, to pass value to environment
        }
    }
}

Here is the code in my main file. It links to the login view so that when the person logs in then it will take them to the content view that has all the other stuff including the code that I have posted before.


//  idAppApp.swift

//  idApp

//

//



import SwiftUI



@main

struct idAppApp: App {

    @StateObject private var wallet = Wallet()

    var body: some Scene {

        WindowGroup {

            LoginView()

        }

    }

}



final class Wallet: ObservableObject {

    @Published var cards = cardsData

    

    var selectedCard: Card {

        cards.first(where: { $0.isSelected})!

    }

}

You did not read my previous answer.

You need to put wallet in the environment:

struct idAppApp: App {

    @StateObject private var wallet = Wallet()

    var body: some Scene {
        WindowGroup {
            LoginView()
                .environmentObject(wallet)         // Essential, to pass value to environment
        }

    }

}
hread 1: Fatal error: No ObservableObject of type Wallet found.
 
 
Q