Learn Courses My Dashboard

Firebase Tutorials (help!): Using Delete Data Function in a different View?

Hello there,
I followed the video on working with data in Firestore and all worked well.

My challenge now is to alternate what I’ve learned and use the add and delete functions in different views. I figured I need the @EnvironmentObject for this and managed to add new items through a different view, that you can see right away when going back to the list view. (I’m proud of this, as it took me a long time!)

What I can’t get to work is deleting items through a different view, as I do not fully understand what parameter to put in the function for it to understand what item to delete exactly.

In the video Chris put item as a parameter, as it sits in a list. In my case, the function sits in a Detail View.
My thinking: item is kind of an id, so I should also work with an Id to tell the function what to delete.
My error message: Cannot convert value of type ‘String’ to expected argument type 'Model’

What is the expected argument type?

Detail View (receiving data from List View, includes the delete function)

struct xDetail: View {
    
    @EnvironmentObject var model: xListModel
    
    var name: String
    var xName: String
    var xId: String
    
    var body: some View {
        
        VStack {
            // show data received from list view (working)
            HStack {
                Text(name)  
                Spacer()
                Text(xName)
            }

            Button {
                // delete item (not working)
                model.deleteX(xDelete: xId) // Cannot convert value of type 'String' to expected argument type 'Model'
            } label: {
                Text("Delete")
            }
        }
    }
}

List View (passing data to the Detail View)

struct xList: View {
    
    @EnvironmentObject var model: xListModel
    
    @State var name = ""
    @State var xName = ""
    @State var xId = ""
    
    var body: some View {
              
        ForEach(model.list) { item in
            NavigationLink(destination: xDetail(name: item.name, xName: item.xName, xId: item.id)) {
                Text(item.name)
            }

        AddButton()
            
        }
    }
}

Model

struct Model: Identifiable {
 
    var id: String
    var name: String
    var xName: String
    
}

Hope you can follow my long explanation and I hope someone can help me understand what I am doing wrong here :slight_smile: