swiftui navigationStack initialising path with onAppear not displaying correct screen

I’m new to swiftui development. Trying to build my first real app! I’m still tackling navigation and authentication where I’m trying to display the correct screens whether the authState is unauthenticated or authenticated. however, my app isn’t launching on the correct screen depending on the state it just crashes with no error message given.

I have a Router class with a single Routes enum for all my routes.

final class Router: ObservableObject {
  public enum Routes: Hashable {
    case landing
    case emailAuth
    case home
  }

  @Published var path: [Routes] = []
  
  func navigate(to destination: Routes) {
    path.append(destination)
  }
  
  func navigateBack() {
    path.removeLast()
  }
  
  func navigateToRoot() {
    path.removeLast(path.count)
  }
}

this is my AuthViewModel where I’m using firebase addStateDidChangeListener to listen for state changes:

enum AuthState {
  case unAuthenticated
  case authenticated
}

@MainActor
class AuthViewModel: ObservableObject {    
  @AppStorage("email") var emailStore: String?
  @Published var email = ""
  @Published var errorMessage = ""
  
  @Published var authState: AuthState = .unAuthenticated
  @Published var user: User?
  @Published var userEmail = ""
  
  init() {
    registerAuthStateHandler()
  }
  
  private var authStateHandle: AuthStateDidChangeListenerHandle?
  
  func registerAuthStateHandler() {
    if authStateHandle == nil {
      authStateHandle = Auth.auth().addStateDidChangeListener{auth, user in
        self.user = user
        self.userEmail = user?.email ?? "(unknown)"
        
        if (user != nil) {
          self.authState = .authenticated
        } else {
          self.authState = .unAuthenticated
        }
        print("Curr authState: (self.authState)")
      }
    }
  }
}

...

I think I’m correctly declaring my router with @StateObject within my view?:

struct ContentView: View {
  @StateObject var authViewModel: AuthViewModel = AuthViewModel()
  @StateObject var router: Router = Router()
  
  var body: some View {
    NavigationStack(path: $router.path) {
//    Text("Loading...")
      LandingScreen(path: $router.path)
        .navigationDestination(for: Router.Routes.self) { route in
          switch route {
          case .landing:
            let _ = print("landing")
            LandingScreen(path: $router.path)
          case .emailAuth:
            AuthEmail(path: $router.path)
          case .home:
            Text("TODO: home page")
          }
        }
    }
    .onAppear {
      print("Curr authState: (authViewModel.authState)")
      if (authViewModel.authState == .authenticated) {
        router.path = [.home]
      } else {
        router.path = [.landing]
      }
      print(router.path)
    }
    .environmentObject(router)
    .environmentObject(authViewModel)
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
      .environmentObject(AuthViewModel())
      .environmentObject(Router())
  }
}

within my subviews, I have something like:

struct LandingScreen: View {
  @EnvironmentObject var authViewModel: AuthViewModel
  @Binding var path: [Router.Routes]
  
    var body: some View {
       // ... 
        NavigationStack {
            NavigationLink(value: Router.Routes.emailAuth) {
              Text("Continue with email")
            }
        }
    }

what I have tried was changing the initial screen in my NavigationStack to just a simple Text("Loading...") however, my screen stays on the “loading…” screen and does not change the screen to the LandingScreen. despite my print logs saying that it has set the path correctly:

Curr authState: unAuthenticated
[app.Router.Routes.landing]
landing

I have also tried moving my onAppear code to an init() instead but it’s expected that init is called multiple times and still stays on the loading screen so that’s a no go…
I have also tried moving the init() up to the Router but I’m getting the same issue.

New contributor

user25078877 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật