When to use Provider.of vs. Consumer in Flutter

前端 未结 4 581
一整个雨季
一整个雨季 2020-12-08 19:42

I\'m still wrapping my head around state-management techniques in flutter and am a bit confused about when and why to use Provider.of vs. Consume

4条回答
  •  不思量自难忘°
    2020-12-08 20:15

    There should not be any performance concern by using it, moreover, we should use consumers if we want to change some specific widget only on screen. This is the best approach I can say in terms of coding practice.

     return Container(
        // ...
        child: Consumer(
          builder: (context, person, child) {
            return Text('Name: ${person.name}');
          },
        ),
      );
    

    Like in the above example, we only required to update the value of Single Text Widget so add consumer there instead of Provider which is accessible to others widget as well.

    Note: Consumer or Provider update the only reference of your instance which widgets are using, if some widgets are not using then it will not re-drawn.

提交回复
热议问题