2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Flutter]providerの情報取得方法

Last updated at Posted at 2024-03-15

こんにちは。
今回は、RiverPodの備忘録を書きます。

概要

スクリーンショット 2023-08-13 20.18.51.png

RiverPodでproviderの情報を取得するには、3つの方法があります。

  • ref.watch
  • ref.read
  • ref.listen

ref.watch

providerの変更を監視する際に、使用します。

ref.watch([ 取得するprovider ])

ref.read

providerの監視が不要な場合に使用します。
また、値を更新する際に使われたりします。
buildの中に記載するとエラーの原因になるそうです。
以下参考。
https://zenn.dev/welchi/scraps/502ce58d996ca5

ref.read( [取得するprovider] )

ref.listen

provider更新を監視し、更新タイミングで何かしらの処理を実行したい時に使います。
例えば、 例えばダイアログで、providerで管理した値の変化を取得するときです。
変更前と変更後を取得できるので、「〇〇を[変更前]→[変更後]に更新しました」と表示することができます。

ref.listen([ 取得したいprovider ], ([ 変更前provider ], [ 変更後provider ]){ [実行したい処理] })

最後に

ここまで読んでいただき、ありがとうございました!
いいねしてくれたら、スキップして喜びます:heartpulse:

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?