ios – How can I retrieve data from SwiftData


im having a problem with swift data , so as you can see in this pic
enter image description here , I want to print out the user
information after he sign in , I have a sign up view and it stored the
data in my data class (swift data) really well but idk how do I cal
them in other view

import SwiftUI
import SwiftData

struct ProfileView: View {
    @Environment (\.modelContext) private var Context
    @Environment (\.dismiss) private var dismiss
    @Query(sort: \data.username) var profile: [data]
//    @State var isLoggedIn: Bool
    
    var body: some View {
        VStack{
            VStack{
                ZStack{
                    Color.gg.ignoresSafeArea().frame(width: 400,height: 250)
                    
                    Spacer()
                    
                    Spacer()
                    Image("logo")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 180, height: 100)
                        .padding()
                    
                    Image(systemName: "camera.circle")
                        .font(.system(size: 30))
                        .foregroundColor(.blue)
                        .offset(x:60, y:70)
                    Spacer()
                    
                        // Text(profile.username)
                        .font(.title)
                        .bold()
                    Spacer()
                }
            }
            Spacer()
            List {
               /* Section(header: Text("Account Information")){*/
                    Text("Email:")
                   // Text(profile.email)
                        .font(.body)
                        .padding()
                    Text("Username:")
                 //   Text(profile.username)
                            .font(.body)
                            .padding()
                    Button(action: {
//                        logout()
                    }) {
                        Text("Logout")
                            .foregroundColor(.red)
                            .font(.headline)
                            .padding()
                           // .background)
                            .cornerRadius(10)
                    }
                    .listRowBackground(Color.white)
            }.listStyle(.plain)
            }
            
            Spacer()
        }
    
    
//    func logout() {
//      
//        email = ""
//        isLoggedIn = true
//    }
    
}
#Preview {
    ProfileView()
}
------------------swift data -----------------------------
import Foundation
import SwiftData

@Model
class data: Identifiable {
    var Excname: String = ""
    var workname: String = ""
    var numofsets: Int = 0
    var numofreps: Int = 0
    var day: String = ""
    let username: String = ""
    let userID: Int = 0
    let email: String = ""
    let password: String = ""

//    init(username: String, password: String, userID: Int, email: String) {
//        self.username = username
//        self.password = password
//        self.userID = userID
//        self.email = email
//    
//    }

    init(username: String, email: String, password: String) {
        self.username = username
        self.email = email
        self.password = password
        
    }

    init(Excname: String, workname: String, numofsets: Int, numofreps: Int, day: String) {
        self.Excname = Excname
        self.workname = workname
        self.numofsets = numofsets
        self.numofreps = numofreps
        self.day = day
    }
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img