添加 NavigationLink 时出现 Swiftui 问题

Swiftui issue when I add a NavigationLink

我想绑定 2 个 swiftui 视图时遇到问题。一个显示发票的创建,另一个显示发票项目。创建发票后,使用 api 上传它以访问责任系统,因此我使用 JSON 创建它(发票)我用来与责任系统通信,因此发票项目。当我想在发票的创建和项目的详细信息之间添加 NavigationLink 时,我失败了。 这是我的代码

这是发票项目的 json 表示

    // MARK: - ItemsFactura
struct ItemFactura: Codable {
    var descripcion, unidadMedida, ccosto1, ccosto2: String?
    var cantidad, importeUnitario, iva: Double?
    var idPlanCuenta, codigo, almacen: String?

    enum CodingKeys: String, CodingKey {
        case descripcion = "Descripcion"
        case unidadMedida, ccosto1, ccosto2
        case cantidad = "Cantidad"
        case importeUnitario = "ImporteUnitario"
        case iva = "IVA"
        case idPlanCuenta, codigo, almacen
    }
}

这是我创建发票的地方:

    final class DummyItemFactura:ObservableObject, Identifiable {
    @Published var id:Int?
    @Published var item:ColppyApiRequests.ItemFactura?
}

struct CreateInvoice: View {

    @State private var showingInvoiceNumber = false
    @State private var showingProveedores = false
    @State private var showingItemsFactura = false
    @State private var isHacienda = false
    @State private var isMercado = false

    @State private var fechaFactura = Date()
    @State private var fechaContable = Date()
    private var tipoDeFactura = ["A", "B", "C", "M", "F", "E"]
    @State private var seleccionTipoDeFactura = ""
    @State private var nroDeFactura = ""
    @State var proveedorSeleccionado:ColppyApiResponses.ListarProveedorResponseData?
    @State var numeroDeFactura:String?
    @State private var fechaDeCompra = Date()
    var lugaresDeCompra = ["Mercado", "Remate Feria", "Directo"]
    @State var itemsFactura = [DummyItemFactura]()
    @State private var lugarDeCompra = 0

    func removeRows(at offsets: IndexSet) {
        itemsFactura.remove(atOffsets: offsets)
    }

    var  body: some View {
        Form {
            Group {
                Button("Seleccionar proveedor"){
                    self.showingProveedores.toggle()}
                    .sheet(isPresented: $showingProveedores) {
                        SelectProveedor(isPresented: self.$showingProveedores,
                                        proveedorSeleccionado: self.$proveedorSeleccionado)}
                Text(self.proveedorSeleccionado?.nombreFantasia ?? "Proveedor seleccionado")
                    .font(.system(size: 15, weight: .light, design: .default))
                Toggle("Hacienda", isOn: $isHacienda)
                Picker(selection: $lugarDeCompra, label:Text("Lugar de compra")) {
                    ForEach(0..<self.lugaresDeCompra.count) {
                        Text(self.lugaresDeCompra[[=11=]]).tag([=11=])
                    }
                }.pickerStyle(SegmentedPickerStyle())
                    .disabled(!isHacienda)
                DatePicker(selection:$fechaDeCompra, displayedComponents: .date) {
                    Text("Fecha de compra")
                }.disabled(!isHacienda)
                HStack {
                    Text("Items factura")
                    Spacer()
                    Image(systemName: "plus.circle")
                        .onTapGesture {
                            print("add item to the array")
                            self.itemsFactura.append(DummyItemFactura())
                    }.padding(10)
                }
                List {
                    ForEach(itemsFactura) {
                            RowInvoiceItem(invoiceItem: [=11=])
                    }.onDelete(perform: removeRows)
                }
                HStack {
                    Text("Importe Gravado")
                    Spacer()
                    Text(".448,58")
                }
                HStack {
                    Text("Importe No Gravado")
                    Spacer()
                    Text("2,27")
                }
            }
            Group {

                DatePicker(selection: $fechaFactura, displayedComponents: .date) {
                    Text("Fecha de la factura")
                }
                DatePicker(selection: $fechaContable, displayedComponents: .date) {
                    Text("Fecha contable")
                }
                Picker(selection: $seleccionTipoDeFactura, label: Text("Tipo de Factura")){
                    ForEach(0..<self.tipoDeFactura.count) {
                        Text(self.tipoDeFactura[[=11=]]).tag([=11=])
                    }
                }
                Button("Ingresar numero de factura") {
                    self.showingInvoiceNumber.toggle()
                }
                .sheet(isPresented: $showingInvoiceNumber) {
                    InvoiceNumberView(isPresented: self.$showingInvoiceNumber, numeroDeFactura:self.$numeroDeFactura)
                }
                Text(numeroDeFactura ?? "Numero de factura")
            }
        }
    }
}

struct CreateInvoice_Previews: PreviewProvider {
    static var previews: some View {
        CreateInvoice()
    }
}

这是 RowInvoiceItemView

struct RowInvoiceItem: View {

    var invoiceItem: DummyItemFactura

    var body: some View {
        VStack() {
            Text(invoiceItem.item?.descripcion ?? "No item" )
                .font(.system(size: 14, weight: .semibold,
                design: .monospaced)).padding(10)
            Spacer()
            HStack {
                HStack {
                    Text("Cant: \(invoiceItem.item?.cantidad ?? 0, specifier: "%.2f")")
                    Text(invoiceItem.item?.unidadMedida ?? "No item")
                }
                Spacer()
                Text("ImpUnit: \(invoiceItem.item?.importeUnitario ?? 0, specifier: "%.2f")")
                Spacer()
                HStack {
                    Text("IVA: \(invoiceItem.item?.iva ?? 0, specifier: "%.2f")")
                    Text("%")
                }
                Spacer()
            }.font(.system(size: 10, weight: .light, design: .monospaced))
            Spacer()
        }
    }
}

这是我添加视图以编辑发票项目的地方:

struct AddingItemFactura: View {


    var itemFactura:ColppyApiRequests.ItemFactura?
    @State private var showingSeleccionarCuentaContable = false
    @State private var descripcion = ""
    @State private var unidadDeMedida = ""
    @State private var cantidad = ""
    @State private var precioPorUnidadDeMedida = ""
    @State private var IVA = 0
    @State private var cuentaContable:ColppyApiResponses.ListarCuentasContablesResponse.CuentaContable?
    @State private var subtotal = 0.0000
    private var ivas = ["0", "2.5", "5", "10.5", "17.1", "21", "27"]


    var body: some View {
        Form {
            TextField(itemFactura?.descripcion ?? "Descripcion", text: $descripcion)
                .keyboardType(.decimalPad)
            TextField("Unidad de medida", text:$unidadDeMedida)
            TextField("Precio por unidad de medida", text:$precioPorUnidadDeMedida)
                .keyboardType(.decimalPad)
            Text("Alicuota de IVA")
            Picker(selection: $IVA, label:Text("Alicuota del IVA")) {
                ForEach(0..<self.ivas.count) {
                    Text(self.ivas[[=13=]]).tag([=13=])
                }
            }.pickerStyle(SegmentedPickerStyle())
            Button("Seleccionar Cuenta Contable") { self.showingSeleccionarCuentaContable.toggle() }
                .sheet(isPresented: self.$showingSeleccionarCuentaContable){
                    SelectCuentaContable(isPresented: self.$showingSeleccionarCuentaContable, cuentaContableSeleccionada: self.$cuentaContable)
            }
            Text(cuentaContable?.Descripcion ?? "Cuenta contable")
            Text("Subtotal : \(subtotal)")
            //Text("IVA: \()")
        }
    }
}

struct AddingItemFactura_Previews: PreviewProvider {

    static var previews: some View {
        AddingItemFactura()
    }
}

当我在此列表中添加 NavigationLink 时出现各种错误

如果有人能告诉我如何解决这个问题,我将不胜感激。 我尝试遵循苹果的流程,但我不知道如何添加(以及在哪里)环境对象。

谢谢

改用显式参数定义

List {
    ForEach(itemsFactura) { item in
       NavigationLink(destination: AddingItemFactura()) { // another context
            RowInvoiceItem(invoiceItem: item) // must be passed explicitly
       }
    }.onDelete(perform: removeRows)
}