2022年6月16日星期四

Provider 比喻 (state management)

 在widget tree最根最頂的global variable/class, variable更新時會通知下層更新


Reading a value 

The easiest way to read a value is by using the extension methods on [BuildContext]:

  • context.watch<T>(), which makes the widget listen to changes on T
  • context.read<T>(), which returns T without listening to it
  • context.select<T, R>(R cb(T value)), which allows a widget to listen to only a small part of T.

One can also use the static method Provider.of<T>(context), which will behave similarly to watch. When the listen parameter is set to false (as in Provider.of<T>(context, listen: false)), then it will behave similarly to read.

沒有留言:

發佈留言