使用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);
},
);
},
);