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

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

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

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

Dart

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

Q&A

解決済

1回答

1331閲覧

RichTextにランダムに選択された文字列を描画したい

9kai

総合スコア14

Flutter

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

Dart

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

1グッド

0クリップ

投稿2020/02/23 06:35

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
popobot👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

リストの宣言が正しくないと思います。文字列のリストは List<String> です。

以下のようにすればいいと思います。

List<String> words = ['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', ];

他の部分もややおかしくなっていたので、動くように直してみたコードを貼っておきます

import 'dart:math'; import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'trigger+words', home: AppMain(title: 'trigger+words'), ); } } class AppMain extends StatefulWidget { AppMain({Key key, this.title}) : super(key: key); final String title; @override _AppMainState createState() => _AppMainState(); } class _AppMainState extends State<AppMain> { String _nowword; //配列からランダムに文字を選ぶ関数 choice() { setState(() { List<String> words = [ '', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', ]; _nowword = words[Random().nextInt(words.length)]; }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ RaisedButton( onPressed: () { choice(); }, child: Text('単語'), ), //問題箇所、シミュレーターを起動するとnullと表示、ボタンを押すと //Another exception was thrown: type 'List<String>' is not a subtype of type 'String' in type cast //と表示が出るものの、検索しても理解できる記述が見つけられない。 RichText( text: TextSpan( style: DefaultTextStyle.of(context).style, children: <TextSpan>[ TextSpan( text: _nowword, style: TextStyle(color: Colors.red), ), ], ), ) ], ), ), ); } }

投稿2020/02/23 07:52

popobot

総合スコア6586

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

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

9kai

2020/02/23 11:28

回答頂きありがとうございます。 文字列のリストは宣言が必要だと知りませんでした。 その他不備のあるコードも改善案を具体的に示して頂き大変助かりました。 誠にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問