【问题标题】:App crashes when loading networkimage in gridview在gridview中加载networkimage时应用程序崩溃
【发布时间】:2019-06-18 08:22:13
【问题描述】:

我正在使用未来创建图像网格视图,但在滚动网格视图应用程序崩溃期间,没有错误,只是

与设备的连接中断。

退出(sigterm)

上下滚动时图像会重新出现和消失。

我尝试过使用 image.network、缓存的网络图像、淡入淡出的图像但无法正常工作。我也将我的图像请求限制为 40,但它也崩溃了。图像大小只有 20-100 kb 范围。

// future function to load subcategory json
Future<subcategory> fetchallsubcategory() async {
  final path = (await getApplicationDocumentsDirectory()).path;
  final myDataResource = HttpNetworkResource(
    url: serverurl +
        "classes/subCategory?limit=40",
    parser: (contents) => json.decode(contents),
    headers: header,
    cache: FileResource(File('$path/subcategoryall.json')),
    maxAge: Duration(minutes: 10),
    strategy: CacheStrategy.cacheFirst,
  );
  final myData = await myDataResource.get();
  return subcategory.fromJson(myData);
}

//grid view
FutureBuilder(
          future: fetchallsubcategory(),
           
          builder: (BuildContext context, AsyncSnapshot snapshot) {
           return snapshot.hasData ? GridView.builder(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 2),
                        itemCount: snapshot.data.results.length,
                        // padding: EdgeInsets.all(2.0),
                        // scrollDirection: Axis.horizontal,
                        itemBuilder: (context, int) {
                          return Card(
                            child: 
                           CachedNetworkImage(imageUrl : snapshot.data.results[int].image),
                          );
                        }
            ) :CircularProgressIndicator();
          },
        )

【问题讨论】:

  • 你有什么解决办法吗?我也面临同样的情况。
  • Mr.saroj 我不确定你是否还在这里在线,但我也有同样的问题.. 如果你有答案的话可以给我答案.. 我会很感激的给你,最好的问候

标签: flutter


【解决方案1】:

我创建了相同类型的应用程序。我发现 NeverScrollableScrollPhysics() 对于可滚动的图像网格视图非常有帮助。希望下面的sn-p代码对你有帮助。

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Column(
          children: <Widget>[
            ConstrainedBox(
              constraints: BoxConstraints(
                minHeight: 10, // Set as you want or you can remove it also.
                maxHeight: double.infinity,
              ),
              child: Container(
                child: GridView.count(
                  crossAxisCount:
                      MediaQuery.of(context).orientation == Orientation.portrait
                          ? 3
                          : 4,
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  physics: NeverScrollableScrollPhysics(),
                  childAspectRatio: .6,
                  children: thumbUrls
                      .map((urlThumb) => Card(
                            child: Container(
                                decoration: BoxDecoration(color: Colors.white),
                                child: GestureDetector(
                                  onTap: () => Navigator.push(context,
                                          new MaterialPageRoute(
                                              builder: (context) {
                                        return new FullScreenImagePage(wallpapers[urlThumb]);//Map wallpaper = {url_thumb : [id, url_image]}
                                      })),
                                  child: new Image.network(
                                    urlThumb,
                                    fit: BoxFit.cover,
                                  ),
                                )),
                          ))
                      .toList(),
                ),
              ),
            )
          ],
        ),
      ],
    );
  }
}

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 2014-05-04
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多