SlideShare a Scribd company logo
1 of 11
Download to read offline
2022年12月17日 寺嶋 誠
Riverpodを使った状態管理
1
目次
自己紹介
Riverpodとは
ソースコード例
併用ライブラリ・パッケージ
*パッケージの「Provider」と、Riverpodで使用する「Provider」という言葉が重複するため
パッケージの方は「旧Provider」としています。
2
参考文献・サイト
https://qiita.com/datake914/items/f91acf30a640447c57c8
https://zenn.dev/riscait/books/
fl
utter-riverpod-practical-introduction
自己紹介
名前 寺嶋 誠(てらしま まこと)
エンジニア歴 2年7ヶ月
使用言語 Flutter, Swift, Vue, Go
職歴
2010年4月 西日本旅客鉄道株式会社 入社
2020年1月 同社 退職
2020年2月 テックエキスパート入校
2020年4月 テックエキスパート卒業
2020年5月 エンジニアに転職
3
Riverpodとは
Dart, Flutterで使用できる「状態管理ライブラリ」
旧Providerパッケージの上位互換
旧Provider + State Notifierの課題を解決
Riverpodを使うメリット
・Widgetの再構築時に状態を失うことなく、状態を安全に作成・監視・破棄できる
・グローバル変数として定義するため、複数の場所から、簡単に状態へアクセス可能になる
・状態の変化によって影響を受けるWidgetのみが再構築(Rebuild)される
など
4
ソースコード例(Provider)
・6行目
このProviderで管理する情報を指定。
(Member)
・23行目
Viewから受け取った情報をcopyWithでセット
する。(freezedを併用しています)
セットするだけで変更通知が行われる。
* 旧Providerの notifyListeners( ) の役割
・27行目
state管理している情報はstate.<変数名>で
呼び出す。
・33行目
外部からmemberProviderを参照できるよう、
グローバル変数化。
5
ソースコード例(View)
・7行目, 11行目
Providerを利用するためのrefを取得するため、
ConsumerWidgetを継承する。
・17行目
refを通じてProviderを利用する。
watchで監視している。
*listenでも監視できるが、値の変化はwatchで
追う。
・29行目
ref.readで、その時点でのProviderを呼び出す。
readは単純にProviderを呼び出すだけなので、
ユーザー操作で呼び出すことが望ましい。
*監視はできないため、watchとの扱い方に違い
が生まれる。
6
*Providerの呼び出し方
7
ref.watch ref.listen ref.read
値の変化を監視する 値の変化は監視しない
監視した値が変化するたびに、値に依存する
ウィジェットやProviderの更新が行われる
監視した値が変化するたびに呼び出される
任意の関数を登録することができる。
(画面遷移、ダイアログの表示など、
変化に応じて処理を実行したいときに有効)
ref.readメソッドを使った時点における
Providerを取得することができる。
取得できるだけで、値の変化は監視できない。
↓
使用地点でのステート取得になるため、
ユーザーが何か操作をしたとき
(ボタンをタップするなど)に呼び出すことが
望ましい。
※実装時は可能な限り ref.watch を使う。
↓
よりリアクティブかつ宣言的になり、
コードの保守性を高めることができる
(公式リファレンスより)
※リアクティブではないため、可能な限り使用を避ける。
↓
watchやlistenの使用で問題が生じる場合の回避策
(公式リファレンスより)
併用ライブラリ・パッケージ
freezed(https://pub.dev/packages/freezed)
flutter̲hooks(https://pub.dev/packages/flutter̲hooks)
8
併用ライブラリ・パッケージ
freezed
flutter̲hooks
導入した理由
・Immutableなクラスが作れる
・copyWith, ==, toStringなどのよく使うメソッドが自動生成できる
・軽微な追記でfromJson, toJsonが自動作成され、クラスとJSONの相互変換が簡単に行える
→ APIから取得してきたJSONファイルなどのマッピングもラクになる
Riverpodと相性がいい
9
併用ライブラリ・パッケージ
freezed
flutter̲hooks
導入した理由
・Widget内のみで状態管理したい場合に便利
・状態が変更された際の検知も簡単に行える
使用hooks
・useEffect
・useState
・useMemoized
・useValueChanged
など
10
併用ライブラリ・パッケージ
・26, 27行目
useStateで初期値をセットする。
・29行目
useEffectを使うと、その中に存在する処理はビルド時 or 第
二引数の値が変化した場合に実行される。
・30行目
useStateで定義した値は、<変数名>.valueで呼び出すことが
できる。代入するだけで上書きも可能。
・36行目
第二引数に監視する値を入れておくことで、変更があった際に
もuseEffect内の処理を走らせることができる。
使用例
11

More Related Content

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

221217_flutter勉強会資料_寺嶋.pdf