'Thread 1: Fatal error: No ObservableObject of type SharedDataModel found. A View.environmentObject may be missing as an ancestor of this view

I have another project with almost identical code and it runs with no issues i'm not sure why it crashes. It build successfully then it crashes with Fatal error on the if statement line.

     if sharedData.likedAwards.isEmpty {
                        
                        Group {
                            Image("NoAwards")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .padding()
                                .padding(.top,35)
                        
                            Text("No awards yet")
                                .font(.custom(customFont, size: 25))
                                .fontWeight(.semibold)
                            
                        
                        }

The Following Code is in SharedDataModel:

import SwiftUI

class SharedDataModel: ObservableObject {
    
    // Detail Award Data....
    @Published var detailAward: Award?
    @Published var showDetailAward: Bool = false
    
    // matched Geoemtry Effect from Search page...
    @Published var fromSearchPage: Bool = false
    
    // Liked Awards...
    @Published var likedAwards: [Award] = []
    
    
}


Solution 1:[1]

use @StateObject var sharedData = SharedDataModel() to create instances of your object on the view to be conform

struct ContentView : View {

@StateObject var sharedData = SharedDataModel()

var body: some View {
Vstack{
if sharedData.likedAwards.isEmpty {
                    
                    Group {
                        Image("NoAwards")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .padding()
                            .padding(.top,35)
                    
                        Text("No awards yet")
                            .font(.custom(customFont, size: 25))
                            .fontWeight(.semibold)
                        
                    }

                    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AdR