【问题标题】:SwiftUI - Fatal error: each layout item may only occur once: file SwiftUI, line 0SwiftUI - 致命错误:每个布局项只能出现一次:文件 SwiftUI,第 0 行
【发布时间】:2021-01-23 19:09:07
【问题描述】:

好的,在此之前将其标记为重复,我在 StackOverFlow 甚至 Apple 论坛上查看过类似的帖子,并实施了所有推荐的解决方案,但问题仍然存在。我使用的是 LazyVGrid。我的代码如下:

这是我的自定义滚动视图,允许在用户到达滚动末尾时进行分页:

                if !quickSearchViewModel.isLoading && !quickSearchViewModel.search_result.isEmpty {
                    DataFetchingScrollView(.vertical, alignment: .center, onOffsetChange: { (off, height) in
                        offset = off
                        heightMinusOffset = height
                        if heightMinusOffset <= UIScreen.main.bounds.height &&
                            !quickSearchViewModel.search_result.isEmpty {
                            quickSearchViewModel.paginate_searchterm {
                                print("Paginated")
                            } onError: { (error) in
                                print(error)
                            }
                        }
                    }) {
                        createGrid()
                            .id(UUID())
                    }

这是我创建网格和网格内视图的两个函数:

private func createGrid() -> some View {
    LazyVGrid(columns: columns) {
        ForEach(quickSearchViewModel.search_result, id: \.product_uid) { product in
            createProductItemView(product)
                .id(product.product_uid)
            
        }
    }
}
private func createProductItemView(_ product: ProductModel) -> some View {
    ProductItemView(product: product)
        .id(product.product_uid)
}

是的,我知道我已经向 id 发送了垃圾邮件,但我已将“.id”单独添加到所有视图中,问题仍然存在。只要我点击搜索,内容就会加载并出现在网格中,这就是我的应用程序崩溃的时候。

编辑 - product.product_uid 是 Firebase 生成的自动 ID。我确实在其他视图中使用了相同的方法,有些工作没有问题,有些可能有小错误。

【问题讨论】:

  • 自从我上传到 Firebase 并且每次自动生成一个新 ID 时,我都不知道如何。这将使这样做几乎不可能。我将查看我的数据库,看看是否有两个相同的 ID,尽管我非常怀疑。

标签: swiftui


【解决方案1】:

发现这个问题,是因为我在查询数据的时候使用了orderBy

【讨论】:

  • 使用 orderBy 有什么问题?我现在面临同样的问题
  • @eugene_prg 我把它归结为我在第一次调用中没有使用 orderBy 但是,如果您通过其他字段查询,这也可能归结为。我无法给出确切的原因。
猜你喜欢
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多