ネットの情報を参考にほぼコピペでコードを書いたのですが、文法のエラーは出ていないものの、該当のページを開くと次のエラーが出てきます。「RangeError (index): Invalid value: Valid value range is empty: 0」
下の画像の「日本」コレクションにある「title」フィールドの値をすべてリストに入れてTextで表示したいと考えています。
Dart
1class ProductModel { 2 String title; 3 ProductModel( 4 this.title, 5 ); 6} 7 8class ProductListModel extends ChangeNotifier { 9 List<ProductModel> products = []; 10 11 Future getProducts() async { 12 var collection = await FirebaseFirestore.instance.collection("日本").get(); 13 products = 14 collection.docs.map((doc) => ProductModel(doc['title'])).toList(); 15 this.products = products; 16 notifyListeners(); 17 } 18}
Dart
1class Nihon extends StatefulWidget { 2 3 State<Nihon> createState() => _NihonState(); 4} 5 6class _NihonState extends State<Nihon> { 7 8 Widget build(BuildContext context) { 9 return Scaffold( 10 appBar: AppBar( 11 title: Text("日本"), 12 ), 13 body: ChangeNotifierProvider( 14 create: (_) => ProductListModel()..getProducts(), 15 child: Consumer<ProductListModel>( 16 builder: (context, model, child) { 17 final products = model.products; 18 return SafeArea( 19 child: Column(children: <Widget>[ 20 Text(products[0].title), 21 Text(products[1].title), 22 ]), 23 ); 24 }, 25 ), 26 ), 27 ); 28 } 29}
エラーを解決するためには上のコードにどのような修正が必要でしょうか?
力を貸していただきたいです。
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/20 16:37
2022/07/20 20:23
2022/07/21 07:53