首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flutter:如何在flutter "ListView.builder()“中限制获取图像?

Flutter:如何在flutter "ListView.builder()“中限制获取图像?
EN

Stack Overflow用户
提问于 2019-05-24 04:49:30
回答 1查看 1.4K关注 0票数 2

我从web服务器获取图像数据,但是,总数据是200+ MB,如果我一次加载它,应用程序将崩溃。如何限制获取照片,可能每5张照片。

我使用ListView.builder() - FLutter,并尝试使用缓存图像库

代码语言:javascript
运行
复制
ListView.builder(
  itemCount: dataJSON == null ? 0 : dataJSON.length,
  padding: EdgeInsets.all(16.0),
  itemBuilder: (context, i) {
    Container(
      height: 100.0,
      width: 70.0,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(5),
          topLeft: Radius.circular(5)
        ),
        image: DecorationImage(
          fit: BoxFit.cover,
          image: new CachedNetworkImageProvider(dataJSON[i]['photo'])
        ),
      ),
    ),
  },
)

当我滚动,大概20张照片,然后崩溃,应用程序是退出

我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-24 13:58:24

你可以使用NotificationListener来增加你的ListView的长度,当用户到达它的末端时,这样只会从网络中获取要显示的照片:

代码语言:javascript
运行
复制
     int list_length ;


     @override
     void initState() {
       list_length = dataJSON.length == null ? 0 : 5 ;
       super.initState();
     }

     NotificationListener<ScrollNotification>(
         onNotification: (scrollNotification){
              if(scrollNotification.metrics.pixels == scrollNotification.metrics.maxScrollExtent){
                 setState(() {
                      list_length = list_length + 10 <= dataJSON.length ? list_length += 10 : dataJSON.length ;
                    });
              }
         },
      child: ListView.builder(
      itemCount: list_length,
      padding: EdgeInsets.all(16.0),
      itemBuilder: (context, i) {
        Container(
          height: 100.0,
          width: 70.0,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
            bottomLeft: Radius.circular(5),
            topLeft: Radius.circular(5)
            ),
            image: DecorationImage(
              fit: BoxFit.cover,
              image: new CachedNetworkImageProvider(dataJSON[i]['photo'])
            ),
          ),
        ),
      },
    )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56282734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档