美文网首页
Flutter使用Provider之Cosumer和Select

Flutter使用Provider之Cosumer和Select

作者: 刘铁崧 | 来源:发表于2021-01-09 18:31 被阅读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);
              },
            );
          },
        );
    

    相关文章

      网友评论

          本文标题:Flutter使用Provider之Cosumer和Select

          本文链接:https://www.haomeiwen.com/subject/eaoeoktx.html