SwiftUI 加载列表中的空白屏幕

Blank Screen on Loading List in SwiftUI

我正在尝试在满足条件后的视图中显示列表。如果从 API 收到数据,则屏幕将加载包含显示多个字段的列表的新视图。这里我只显示列表中的一个字段。导航代码工作正常并且数据也被解码但是当单击列表按钮屏幕移动到下一个视图时出现空白屏幕但空白屏幕。

这是我显示列表的视图:

      import SwiftUI

       struct MyPriceList: View {

       @StateObject var road = ListAPI()

        List
        {
            ForEach(road.priceRoad)
            {
                road in
                   HStack{
                           Text(road.packageName)
                           .font(.system(size: 15))
                           .foregroundColor(.black)
                         }
             }
         }
     }  
           
       struct MyPriceList_Previews: PreviewProvider {
         static var previews: some View {
             MyPriceList()
                 }
              }
           }

以下是我解码 JSON 数据并应用导航的视图模型

    import Foundation

    class ListAPI : ObservableObject
          {
                @Published var priceRoad = [ResponseList]()
            func getList()
                  {
                            // url building code //

               let list = URLSession.shared.dataTask(with: urlRequest)
                  {
                      (data, response, error) in
                       if let error = error {
                            print("Error \(error)")
                           }
        
                   if let data = data
                     {
                               do
                         {
                          let jsonList = try JSONDecoder().decode(PriceList.self, from: data)
                            let panama = jsonList.response
                         for pan in panama
                            {
                               print(pan.packageName) //successfully printing
                            }
                            if jsonList.success==true
                               {
                                   DispatchQueue.main.async
                                        {
                                               self.navigate = true
                                               self.priceRoad = jsonList.response                                    
                                        }
                                }
                    else
                         {
                               DispatchQueue.main.async
                               {
                                   self.errorMessage = jsonList.message
                               }
                          }
                     }
            catch
            {
                print("error \(error)")
            }
        }          
    }
    list.resume()
} }
   

这是 Json

的数据模型
      struct PriceList : Codable
               {
                        let success: Bool
                        let message: String
                        let response: [ResponseList]

                      enum CodingKeys:String, CodingKey{
                                   case response = "ResponseData"
                                   case success = "IsSuccess"
                                   case message = "Message"
                         }
              }


               struct ResponseList:Codable
                    {
                         let packageId: Int
                         let packageName: String
                         let price: Double
                         let discountedPrice: Double
                         let testType: String
                         let testPackageGroupId: Int?
                         let SampleType: [SampleTypeList]?    

                            enum CodingKeys:String, CodingKey{
    
                             case packageId = "PackageId"
                             case packageName = "PackageName"
                             case price = "Price"
                             case discountedPrice = "DiscountedPrice"
                             case testType = "Type"
                             case testPackageGroupId = "TestPackageGroupId"
                             case SampleType = "SampleTypeList"
    }}
   struct SampleTypeList:Codable
                 {
                      let testSampleTypeId: String
                      let sampleName: String
                      let colourCode: String

                             enum CodingKeys:String, CodingKey{
                                case testSampleTypeId = "TestSampleTypeId"
                                case sampleName = "SampleName"
                                case colourCode = "ColourCode"

                               }
                   }

我需要将 TestName 显示为 packageName,将 MRP 显示为价格,将 B2B 显示为 discountedPrice,并将 TestType 显示为 testType。

尝试这种方法,在 .onAppear{}:

中调用 road.getList()
struct MyPriceList: View {
    
    @StateObject var road = ListAPI()
    
    var body: some View {  // <-- here need a body
        List
        {
            ForEach(road.priceRoad)
            {
                road in
                HStack{
                    Text(road.packageName)
                        .font(.system(size: 15))
                        .foregroundColor(.black)
                }
            }
        }
        .onAppear {
            road.getList() // <-- here load your data
        }
    }
}

并使 PriceListResponseList Identifiable,像这样:

struct PriceList : Identifiable, Codable {
    let id = UUID()
    // ...
}

struct ResponseList: Identifiable, Codable {
    let id = UUID()
    // ...
}
    
struct SampleTypeList: Identifiable, Codable {
    let id = UUID()
    // ...
}

或者,在 ListAPI 中,您可以使用 init() { getList() },而不是使用 .onAppear {road.getList()}