【发布时间】:2020-07-16 11:05:33
【问题描述】:
我使用 Rxdart-provider 模式(dio 的 http 请求)构建了我的应用程序。当我从服务器获取数据时,通过调用addAll() 方法更新其数据列表后,我的 ListView 未重建;但是,当我尝试 dataList = newDataList 时,它有效!ListView 更新成功。有人可以告诉我为什么。
提供者:
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((_) {})
);
列表视图:
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 flutter dart provider rxdart