Flutterでモバイルアプリを作成しています。
画面にボタンの押下に対応して配列から選んだアルファベットをランダムに表示する機能を実装しようとしています。
現在シュミレーター上ではボタンを押下すると「Another exception was thrown: type 'List<String>' is not a subtype of type 'String' in type cast」とメッセージが出るのみで表示はnullから変化しません。
どのように修正すれば文字列が描画されるようになるでしょうか。
ご教示頂ければ幸いです。
sample.dart
1import 'package:flutter/material.dart'; 2//link 3import 'package:link/link.dart'; 4//random 5import 'dart:math'; 6import 'dart:math' as math; 7//gesture 8import 'package:flutter/gestures.dart'; 9 10void main() => runApp(MyApp()); 11 12class MyApp extends StatelessWidget { 13 // This widget is the root of your application. 14 @override 15 Widget build(BuildContext context) { 16 return MaterialApp( 17 title: 'trigger+words', 18 home: AppMain(title: 'trigger+words'), 19 ); 20 } 21} 22 23class AppMain extends StatefulWidget { 24 AppMain({Key key, this.title}) : super(key: key); 25 final String title; 26 27 @override 28 _AppMainState createState() => _AppMainState(); 29} 30 31class _AppMainState extends State<AppMain> { 32 33 //配列からランダムに文字を選ぶ関数 34 choice(){ 35 setState(() { 36 String words = ['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', ] as String; 37 var new = new Random(); 38 String _newword = words[0]; 39 _nowword = words[now.nextInt(words.length)]; 40 }); 41 } 42 43 @override 44 Widget build(BuildContext context) { 45 return Scaffold( 46 backgroundColor: Colors.white, 47 body: Center( 48 child: Column( 49 mainAxisAlignment: MainAxisAlignment.center, 50 children: <Widget>[ 51 RaisedButton( 52 onPressed: (){choice();}, 53 child: Text('単語'), 54 ), 55 //問題箇所、シミュレーターを起動するとnullと表示、ボタンを押すと 56 //Another exception was thrown: type 'List<String>' is not a subtype of type 'String' in type cast 57 //と表示が出るものの、検索しても理解できる記述が見つけられない。 58 RichText( 59 text: TextSpan( 60 style: DefaultTextStyle.of(context).style, 61 children: <TextSpan>[ 62 TextSpan( 63 text: '$_newword', 64 style: TextStyle(color: Colors.red), 65 ), 66 ], 67 ), 68 )], 69 ), 70 ), 71 72 ); 73 } 74} 75

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/23 11:28