我从web服务器获取图像数据,但是,总数据是200+ MB,如果我一次加载它,应用程序将崩溃。如何限制获取照片,可能每5张照片。
我使用ListView.builder() - FLutter,并尝试使用缓存图像库
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张照片,然后崩溃,应用程序是退出
我该怎么办?
发布于 2019-05-24 13:58:24
你可以使用NotificationListener
来增加你的ListView
的长度,当用户到达它的末端时,这样只会从网络中获取要显示的照片:
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'])
),
),
),
},
)
https://stackoverflow.com/questions/56282734
复制相似问题