How to pass multiple StateObject into Environment Object in Swift

101 views Asked by At

I am developing a shopping cart app . I want to determine when User is logged in that time I want to show MainView (Home view). If user is not logged in that time I am showing LoginView and it working fine when I pass the value into environmentObject but I want to another object which is Order class , which will be observable into different view. I am not sure How can I pass two StateObjects into one environmentObject to make sure order class is not destroyed or recreate it again ..

Error is here ..

.environmentObject(viewModel, order)

Here is my Main view ..

import SwiftUI
import Firebase

@main
struct DemoEcommerceApp: App {
    @StateObject var viewModel = AuthViewModel()
    @StateObject var order = Order()

    init() {
        FirebaseApp.configure()
    }
    var body: some Scene {
        WindowGroup {
           ContentViewOne()
                .environmentObject(viewModel, order)
        }
    }
}

Here is the Content view which has environment variable ..

import SwiftUI

struct ContentViewOne: View {

    @EnvironmentObject var viewModel: AuthViewModel
    
    var body: some View {
        Group {
            if viewModel.userSession != nil {
                MainView()
            } else {
                LoginView()
            }
        }
    }
}

struct ContentViewOne_Previews: PreviewProvider {
    static var previews: some View {
        ContentViewOne()
    }
}

Here is the screenshot..

error

0

There are 0 answers