我用Rxdart-provider模式(http request by dio)建立了我的App。当我从服务器获得数据时,我的ListView在我通过调用更新其数据列表后没有重建。addAll()
但是,当我尝试 dataList = newDataList
谁能告诉我为什么,请问。
提供者:
class ServicesVM extends BaseProvider {
final ServicesRepository _model;
List<ServiceEntity> serviceList = [];
List<Goods> currGoods = [];
ServicesVM(this._model);
void getServices() {
addSubscription(
_model.getServices()
.doOnData((data) {
//now work
serviceList.addAll((data.data as List).map(
(ele) => ServiceEntity.fromJson(ele)
));
//works
/*List<ServiceEntity> temp = [];
temp.addAll((data.data as List).map(
(ele) => ServiceEntity.fromJson(ele)
));
serviceList=temp;*/
serviceList.asMap().forEach((i,v) {
v.selected = i == 0;
});
currGoods = serviceList[0].goods;
}).doOnDone(() {
serviceList.forEach(print);
notifyListeners();
}).listen((_) {})
);
ListView。
Container(
width: 99.dp,
height: double.infinity,
color: Color(Iro.gray4),
child: Selector <ServicesVM, List<ServiceEntity>>(
selector: (context,provider) => provider.serviceList,
builder: (context,value,child){
return ListView.separated(
separatorBuilder: _getSeparator,
itemBuilder: (context,index) {
return _getLabelItem(index,value[index]);
},
itemCount: value.length,
padding: const EdgeInsets.all(0),
physics: NeverScrollableScrollPhysics(),);
},
),
),
我一直在争分夺秒地回答这个问题。我已经被同样的问题卡了几天了。使用ListView.builder()。这样做的原因是Flutter使用的是不可变的对象。所以虽然底层数据发生了变化,但ListView不会重新绘制。