使用Selector和Cosumer都可以不同程度的避免rebuild,下面两个案例,使用Selector最佳
return Selector<MealPModel,List<MealsDetail>>(
selector: (context,model)
{
return model.meals.where((element)
{
return element.id == "m1";
}).toList();
},
shouldRebuild: (pre,next) => pre != next,//判断是否要rebuild
builder: (context,model,child){
return ListView.builder(
itemCount: model.length,
itemBuilder: (context,index){
return Text(model[index].title);
},
);
},
);
return Consumer<MealPModel>(
builder: (context,model,child){
final meals = model.meals.where((element) => element.id == "m1").toList();
return ListView.builder(
itemCount: meals.length,
itemBuilder: (context,index){
return Text(meals[index].title);
},
);
},
);
网友评论