【问题标题】:SwiftUI Search bar unable to product diagnostic errorSwiftUI 搜索栏无法产生诊断错误
【发布时间】:2021-06-01 08:44:21
【问题描述】:

我正在尝试使用本教程实现搜索栏:https://www.appcoda.com/swiftui-search-bar。

我可以让搜索栏显示和获取文本,但它对文本没有任何作用,因为在此阶段它不用于过滤。

当我尝试使用它来过滤我的产品列表(取自 Firestore)时,它会引发 Failed to produce diagnostic for expression; please submit a bug report 错误。

struct ProductList: View {
    @State private var searchText: String = ""
    
    // ProductsModel() is the Class that this structure is looking at. If anything in ProductsModel changes, then - because it is an ObservableObject - then this structure/view ProductList will update with the new information.
    @ObservedObject private var viewModel = ProductsModel()
    
    var body: some View {
        NavigationView{
            VStack{
                
                HStack{
                NavigationLink(destination: ProductCreationView()){Text("Add Product")}
                NavigationLink(destination: Initialinfo()){Text("Help")}
                } //Navigation Links
                
                SearchBar(text: $searchText)
                
                List(viewModel.product_fire.filter({ searchText.isEmpty ? true : $0.product_name.contains(searchText) })){ product in
                    NavigationLink(destination: ProductDetail(product: product))
                    {ItemRow(product: product).navigationBarTitle("Products", displayMode: .inline)}
                    }.onAppear(){
                        self.viewModel.fetchData()
                    }
                
                
                // Create a list of all products where each product is given a link that points to the specific product detail page as well as the description taken from the ItemRow view for that product.
                // This displays as : Product Name/Link
                //                      Product Brand
                //                    ...
                List(viewModel.product_fire, id:\.product_name) { product in
                NavigationLink(destination: ProductDetail(product: product))
                {ItemRow(product: product).navigationBarTitle("Products", displayMode: .inline)}
                }.onAppear(){
                    self.viewModel.fetchData()
                }

如果我注释掉过滤后版本的代码,它会显示正常,并从数据库中提取所有产品,并以名称/链接和品牌作为一对显示每个产品作为一个大列表。

从阅读其他问题 RE 这个错误 - 但没有关于搜索栏 - 这似乎是 { } 放置等问题,但没有任何修补有帮助。

编辑 1: 我创建了一个接收 searchText 的函数,然后在 Firestore 中搜索 documentID 等于 searchText 的文档(这些应该是唯一的)。

func singleData(searchText: String){
    db.collection("Products").whereField("code", isEqualTo: searchText)
        .getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    print("\(document.documentID) => \(document.data())")
                }
            }
    }
}

ProductList 视图中的 List 和 NavigationLinks 已替换为:

        Text("Test").onAppear(){
            self.viewModel.singleData(searchText: String)
        }

如果 searchText 是现有文档,那么它将将该代码传递给ProductDetail 视图并将用户直接带到那里,并消除对列表的需要。 它将产品数据打印到控制台,但我在尝试将值返回到视图时遇到问题,因为它给出了 void 类型错误。

非常感谢。

【问题讨论】:

  • 您是在模拟器还是在设备上运行代码以获得更有意义的错误?
  • 它不会构建并且只是一直标记这个错误,因为在模拟器和真实设备上构建失败的原因
  • 您真的应该将该过滤逻辑移动到您的视图模型中。

标签: swift swiftui


【解决方案1】:

去掉filter中的括号

List(viewModel.product_fire.filter{ searchText.isEmpty ? true : $0.product_name.contains(searchText) }){ product in

但最好只从ViewModel 中获取过滤后的对象

【讨论】:

  • 我的 viewModel 是一个当前只获取数据并将其全部传递给列表的类。我是否认为您建议在将数据传递到列表之前减少班级中的数据?如果是这样,流程是否会采用 searchText,将其传递给 fetchData 函数并仅获取与 searchText 匹配的数据?然后返回给列表使用?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多