前提・実現したいこと
flutter(dart)にて、変数の中に変数を入れてリストを作りたいと考えております
発生している問題・エラーメッセージ
only static members can be accessed in initializersと表示されます
該当のソースコード
flutter
1import 'package:flutter/material.dart'; 2 3void main() => runApp(MyApp()); 4 5class MyApp extends StatelessWidget { 6 // This widget is the root of your application. 7 @override 8 Widget build(BuildContext context) { 9 return MaterialApp( 10 title: 'Flutter Demo', 11 theme: ThemeData( 12 primarySwatch: Colors.blue, 13 ), 14 home: MyHomePage(title: 'Flutter Demo Home Page'), 15 ); 16 } 17} 18 19class MyHomePage extends StatefulWidget { 20 MyHomePage({Key key, this.title}) : super(key: key); 21 final String title; 22 23 @override 24 _MyHomePageState createState() => _MyHomePageState(); 25} 26 27class _MyHomePageState extends State<MyHomePage> { 28 int _counter = 0; 29 30 List<String> listItem = ["国語", "数学", "英語"]; 31 List<String> subject1=["漢文","文法","小論文"]; 32 List<String> subject2=["三角関数","因数分解","数列"]; 33 List<String> subject3=["長文","文法","アクセント"]; 34 List<List<String>> subject4=[subject1,subject2,subject3]; 35 36 37 void _incrementCounter() { 38 setState(() { 39 _counter++; 40 }); 41 } 42 43 @override 44 Widget build(BuildContext context) { 45 return Scaffold( 46 appBar: AppBar(title: Text('勉強動画まとめ'),), 47 body: ListView.builder( 48 itemBuilder: (BuildContext context, int index) { 49 return Container( 50 decoration: BoxDecoration( 51 border: Border( 52 bottom: BorderSide(color: Colors.black38), 53 ), 54 ), 55 child: ListTile( 56 leading: const Icon(Icons.flight_land), 57 title: Text(listItem[index]), 58 subtitle: Text('subject'), 59 onTap: () {Navigator.push( 60 context, 61 MaterialPageRoute(builder: (context) => SecondRoute()), 62 );}, 63 ));}, 64 itemCount: listItem.length, 65 ), 66 67 floatingActionButton: FloatingActionButton( 68 onPressed: _incrementCounter, 69 tooltip: 'Increment', 70 child: Icon(Icons.add), 71 ), 72 ); 73 } 74}
試したこと
List<String>の中にList<String>を入れてみましたがダメでした
List<String> subject1=["漢文","文法","小論文"];
List<String> subject2=["三角関数","因数分解","数列"];
List<String> subject3=["長文","文法","アクセント"];
List<List<String>> subject4=[subject1,subject2,subject3];
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/18 07:44