SwiftData Component

Hello, I'm a little new to Swift and very new to SwiftData. I'm making a flashcard app in which the decks of flashcards are displayed in a view. I've created a reusable component for the decks in which I want to use SwiftData to store the Title date of creation and eventually number of cards. So far I'm just trying to get the title. Here is the component:

Deck Component

import SwiftUI
import SwiftData

struct DeckRow: View {
    @State private var isPressed = false
    @State private var isToggled = false
    @Environment(\.modelContext) var modelContext
    @Query var decks: [Deck]
    
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 20)
                .frame(height: 99)
                .padding(.horizontal)
                .foregroundStyle(Color.primaryC)
                .offset(y: 8)
                
            
            HStack(alignment: .top) {
                VStack(alignment: .leading, spacing: 20) {
                    HStack {
                        Text(decks.name)
                            .font(.title2)
                            .bold()
                        
                        Text("2w")
                            .font(.subheadline)
                            .fontWeight(.light)
                    }
                    
                    HStack {
                        Image(systemName: "square.3.layers.3d.top.filled")
                        Text("20 Cards")
                    }
                }
                
                Spacer()
                
                Button {
                    
                } label: {
                    Image(systemName: "ellipsis.circle")
                        .font(.title3)
                        .bold()
                }
                
            }
            .padding()
            .foregroundStyle(Color.text)
            .background(Color.secondaryC)
            .cornerRadius(15)
            .padding()
            .offset(y: isPressed ? 8 : 0)
            .gesture(
                DragGesture(minimumDistance: 0)
                    .onChanged { _ in
                        withAnimation(.linear(duration: 0.5)) {
                            isToggled.toggle()
                        }
                        isPressed = true
                    }
                    .onEnded { _ in
                        withAnimation(.easeInOut) {
                            isPressed = false
                        }
                    }
            )
        }
       
    }
}

But, Text(decks.name) keeps returningValue of type '[Deck]' has no member 'name' even though it does. Here is the data model:

Data Model

import Foundation
import SwiftData

@Model
class Deck {
    var name: String
    var cards: Int
    var creationDate: Date
    
    init(name: String = "", cards: Int = 0, creationData: Date = Date()) {
        self.name = name
        self.cards = cards
        self.creationDate = creationData
    }
}

Any help or tips would be greatly appreciated. Thank you!

Accepted Answer

Your decks property is an array of type Deck, not a single deck. An array of Deck does not have a name, therefore the following line of code won't work:

Text(decks.name)

You need to show the name of a single deck in the Text label. If you want to show the names of all the decks, create a list and have a Text label for each item in the list.

List(decks) { deck in
    Text(deck.name)
}
SwiftData Component
 
 
Q