Flutter使用Provider之Cosumer和Select

2021-01-09  本文已影响0人  刘铁崧

使用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);
          },
        );
      },
    );
上一篇下一篇

猜你喜欢

热点阅读