質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

Q&A

解決済

1回答

2743閲覧

StatefulWidgetで実装されているアプリのRiverpod化

9kai

総合スコア14

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

0グッド

0クリップ

投稿2021/02/22 08:16

前提・実現したいこと

昨年このサイトでさせて頂いた質問への回答を元にToDoアプリをリリースしました。
現在アプリのコードを従来のStateful Widgetを使用したものからRiverpodを使用したコードに変更したいと考えています。
実装したいのですが、見つけた情報の中で例として示されている情報の大半がデフォルトのカウンターアプルの書き換えで私がしたいこととは異なっており実装ができず詰まっています。

生じている疑問

1.1ファイル200行未満(+NoSQL)で実装されるアプリはRiverpod化する必要・意義があるのか。
2.1ファイルでアプリを作成するのは望ましくないのか。
3.RiverpodでNoSQLにデータを保存するメモアプリのコード例が掲載されているサイトがあるのか。

全てに回答が頂けなくても構いませんのでご教示頂ければ幸いです。

(参照したサイト)
【Flutter】Riverpodを使って状態を管理する

Flutter | Pragmatic Architecture using Riverpod
こちらの記事ではファイルをかなり詳細に分割されているので上記2.の疑問に至りました。

該当のソースコード

dart

1import 'package:flutter/material.dart'; 2//For make icon package. 3import 'package:flutter_launcher_icons/android.dart'; 4import 'package:flutter_launcher_icons/constants.dart'; 5import 'package:flutter_launcher_icons/custom_exceptions.dart'; 6import 'package:flutter_launcher_icons/ios.dart'; 7import 'package:flutter_launcher_icons/main.dart'; 8import 'package:flutter_launcher_icons/xml_templates.dart'; 9//For url_launcher Package. 10import 'package:url_launcher/url_launcher.dart'; 11//For use NoSQL Database package. 12import 'package:shared_preferences/shared_preferences.dart'; // 追記する 13 14//constitution 15//main->Myapp->AppMain->_AppMainState 16 17void main() => runApp(MyApp()); 18 19class MyApp extends StatelessWidget { 20 21 Widget build(BuildContext context) { 22 return MaterialApp( 23 debugShowCheckedModeBanner: false, 24 title: 'tllist', 25 theme: ThemeData( 26 ), 27 home: AppMain(title: 'tllist'), 28 ); 29 } 30} 31 32class AppMain extends StatefulWidget{ 33 34 AppMain({Key key, this.title}) : super(key: key); 35 final String title; 36 37 38 _AppMainState createState() => _AppMainState(); 39} 40 41class _AppMainState extends State<AppMain> { 42 43 //These code is not need before widget. 44 //No problem even after the widget. 45 //However, do not write it outside of _AppMainState~{}. 46 //Prepare for link. https://pub.dev/packages/link 47 _launchURL() async { 48 const url = 'https://9vox2.netlify.com/'; 49 if (await canLaunch(url)) { 50 await launch(url); 51 } else { 52 throw 'Could not launch $url'; 53 } 54 } 55 56 //Prepare for use NoSQL, this code make controller & Variable for recieve updates. 57 final redController = TextEditingController();//Controller for red 58 final yellowController = TextEditingController();//Controller for yellow 59 final greenController = TextEditingController();//Controller for green 60 var isLoaded = false;// 61 62 //Prepare for load(). 63 //There is no problem if there are multiple @overrude. 64 65 void initState() { 66 super.initState(); 67 load(); 68 } 69 70 //load function 71 Future<void> load() async { 72 final prefs1 = await SharedPreferences.getInstance(); 73 redController.text = prefs1.getString('red'); 74 yellowController.text = prefs1.getString('yellow'); 75 greenController.text = prefs1.getString('green'); 76 setState(() { 77 isLoaded = true; 78 }); 79 } 80 81 //save function 82 Future<void> save(key, text) async { 83 final prefs2 = await SharedPreferences.getInstance(); 84 await prefs2.setString(key, text); 85 setState(() { 86 isLoaded = true; 87 }); 88 } 89 90 91 //Widget:All drawings on the screen will be displayed exactly as written here. 92 Widget build(BuildContext context) { 93 return Scaffold( 94 backgroundColor: Colors.white, 95 body: Visibility( 96 visible: isLoaded, 97 child: SingleChildScrollView( 98 child: Column( 99 children: <Widget>[ 100 Text(''), 101 Text(''), 102 Text('tllist'), 103 Text(''), 104 //ASAPfill ASAP is an abbreviation for as soon as possible. 105 TextField( 106 controller: redController, 107 onChanged: (text) { 108 save('red', text); 109 }, 110 minLines: 4, 111 maxLines: 4, 112 maxLength: 150, 113 maxLengthEnforced: true, 114 decoration: InputDecoration( 115 fillColor: Colors.red[200], 116 filled: true, 117 border: OutlineInputBorder(), 118 hintText: '緊急/ASAP', 119 )), 120 //ImportantFill 121 TextField( 122 controller: yellowController, 123 onChanged: (text) { 124 save('yellow', text); 125 }, 126 minLines: 6, 127 maxLines: 6, 128 maxLength: 250, 129 maxLengthEnforced: true, 130 decoration: InputDecoration( 131 fillColor: Colors.yellow[300], 132 filled: true, 133 border: OutlineInputBorder(), 134 hintText: '重要/Important', 135 )), 136 //Otherfill 137 TextField( 138 controller: greenController, 139 onChanged: (text) { 140 save('green', text); 141 }, 142 minLines: 8, 143 maxLines: 8, 144 maxLength: 350, 145 maxLengthEnforced: true, 146 decoration: InputDecoration( 147 fillColor: Colors.green[300], 148 filled: true, 149 border: OutlineInputBorder(), 150 hintText: 'その他/Other', 151 )), 152 Text(''), 153 //Link 154 RaisedButton( 155 onPressed: _launchURL, 156 child: Text('ABOUT'), 157 ), 158 ], 159 ), 160 ), 161 )); 162 } 163}

試したこと

Riverpodの紹介記事を元にコーディングを試みました。

補足情報(FW/ツールのバージョンなど)

Flutter 1.22.6
Dart 2.10.5

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Riverpod好きの私がご質問にお答えします。

1.1ファイル200行未満(+NoSQL)で実装されるアプリはRiverpod化する必要・意義があるのか。

必要かというと、きちんと状態管理(State Managment)ができているのであれば必須ではないと思いますが、StatefulWidgetはコードが増えた時に整理しにくいと個人的には思います。

2.1ファイルでアプリを作成するのは望ましくないのか。

アプリの規模によりますが、基本的には望ましくないです。フォルダやファイルで整理することをお勧めします。

3.RiverpodでNoSQLにデータを保存するメモアプリのコード例が掲載されているサイトがあるのか。

ピンポイントで両方取り扱っている記事は少ないと思いますが、まずそれぞれを理解すれば組み合わせることは容易です。

個人的な結論としては、この程度の規模であればStatefulWidgetで十分だと思いますがコード量が増えるなら事前にRiverpodなどで最適な設計にしておくことをお勧めします。慣れれば上記のコードでしたら10分ほどでRiverpod化できると思います。

投稿2021/02/25 01:02

hiroshihorie

総合スコア192

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

9kai

2021/03/01 01:32

丁寧なご回答ありがとうございます。 今後の方針にさせていただきます。 無理にRiverpod化する必要がないとわかったのは本当に有り難いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問