【问题标题】:Why gridview reload images in Flutter?为什么 gridview 在 Flutter 中重新加载图像?
【发布时间】:2021-12-17 07:26:43
【问题描述】:

我正在尝试在 gridview 中使用 Image.file(File(path)) 的显示图像。当我向下滚动然后向上滚动时,它正在重新加载图像。这导致它们以白色显示。当我在寻找这个问题时,会提到“延迟加载列表”,但我认为这与此无关。我也尝试使用 LruCache 缓存 Image 小部件,但它仍然相同。如何防止这种重新加载?

这是我的代码 sn-p:

  child: GridView.builder(
        itemCount: paths.length,
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          childAspectRatio: 0.5,
          crossAxisSpacing: 6,
          mainAxisSpacing: 6,
          crossAxisCount: 5,
        ),
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
            onTap: () => {remove(paths[index])},
            child: Image.file(File(paths[index])),
          );
        },
      ),

这是一个错误:停止原因 = EXC_RESOURCE RESOURCE_TYPE_MEMORY 颤动

【问题讨论】:

    标签: flutter gridview


    【解决方案1】:
    GridView(gridDelegate: gridDelegate, cacheExtent: 9999,)
    

    【讨论】:

    • 谢谢,但我试过了。当我这样做时,该应用程序崩溃了。
    • 为什么会崩溃?它是否返回任何错误?也许向我们展示一些代码,以便我们知道问题出在哪里。试试 cacheExtent 100 或 200
    • 很抱歉回答迟了。我添加了我的代码 sn-p 以及错误。
    • 是的,这个错误意味着你的应用使用了太多资源,可能我给你的数字有点夸张试试 cacheExtent: 1000 甚至 100
    • 我把 cacheExtent: 1000 但仍然抛出错误。
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多