【问题标题】:Where should you place the state value in Riverpod?您应该将状态值放在 Riverpod 的什么位置?
【发布时间】:2021-06-08 11:54:32
【问题描述】:

访问 StateProvider 或 StateNotifierProvider 的状态:

有时在 Riverpod 文档中,状态变量添加在 watch 函数之后。

int count = watch(counterProvider).state;

但是,我使用 StateNotifier 的代码只有在我在 watch 中引用它时才有效。即

watch(myNotifier.state)

有什么区别?

【问题讨论】:

标签: flutter flutter-provider flutter-state riverpod


【解决方案1】:

使用提供者的小部件在这两种情况下的行为会有所不同。

第一种情况:

watch(counterProvider).state

消费者将查看整个 counterProvider,如果有任何原因导致 NotifyProvider,它将被重建。

第二种情况:

watch(counterProvider.state)

消费者只查看状态变量,只有当状态发生变化并导致 NotifyProvider 时才会重新构建。

【讨论】:

  • 谢谢,如果您认为问题正确,请将问题标记为正确
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 2015-04-10
  • 2012-03-03
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
相关资源
最近更新 更多