When to use Provider.of vs. Consumer in Flutter

前端 未结 4 580
一整个雨季
一整个雨季 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:19

    For your questions:

    1. Is this the correct way to distinguish Provider.of and Consumer. Former doesn't update UI, latter does?

    Provider.of depends on value of listen to trigger a new State.build to widgets and State.didChangeDependencies for StatefulWidget.

    Consumer always update UI, as it uses Provider.of(context), where listen is true. See full source here.

    1. If listen isn't set to false will the widget be rebuilt by default or not rebuilt? What if listen is set to true?

    Default value is true, means will trigger a new State.build to widgets and State.didChangeDependencies for StatefulWidget. See full source here.

    static T of(BuildContext context, {bool listen = true}).

    1. Why have Provider.of with the option to rebuild the UI at all when we have Consumer?

    Pretty much covered by Rémi Rousselet's answer.

提交回复
热议问题