過去の質問の文脈に関連する質問であるのですが、複数の質問については投稿を分けてくださいという指摘を受けたことがあるため重ねてで申し訳ございませんが、ご質問させてください。
//here1
の結果について、DartPadにおいて
[Container(color: Color(0xfff44336)), Container(color: Color(0xff2196f3))]
という結果を期待していて、下記のコードを作成しているのですが、実際には、
[Container(bg: Color(0xfff44336)), Container(bg: Color(0xff2196f3))]
という ドキュメントにもないプロパティに勝手に変更されていることがわかりました。
Dart
1import 'package:flutter/material.dart'; 2import 'dart:math'; 3 4void main() => runApp(TopPage()); 5 6class TopPage extends StatelessWidget { 7 final List<Widget>? _tiles =[ 8 Container( 9 color:UniqueColorGenerator.getColor(), 10 child: Padding(padding: EdgeInsets.all(100.0)) 11 ), 12 Container( 13 color:UniqueColorGenerator.getColor(), 14 child: Padding(padding: EdgeInsets.all(100.0)) 15 ), 16 ]; 17 18 19 20 Widget build(BuildContext context) { 21 print( _tiles); //here1 22 return MaterialApp( 23 home:Scaffold( 24 appBar:AppBar( 25 title:Text("TESTING"), 26 ), 27 body:Row( 28 children: <Widget>[Container(color: Color(0xff2196f3),width: 48.0,height: 48.0,), 29 Container(color: Color(0xfff44336),width: 48.0,height: 48.0,)], //here2 30 ), 31 ) 32 ); 33 } 34} 35 36class UniqueColorGenerator { 37 static List _colorOptions = [ 38 const Color(0xff2196f3), 39 const Color(0xfff44336), 40 ]; 41 static Random _random = Random(); 42 static Color getColor() { 43 final selectedColor = _colorOptions.removeAt(_random.nextInt(_colorOptions.length)); 44 print(selectedColor); 45 return selectedColor; 46 } 47}
その結果、//here2
について以下のようにしたいのですが、
Dart
1 body:Row( 2 children: _tiles, 3 ),
下記のようにエラーが出て型が合わないと言われます。
The argument type 'List<Widget>?' can't be assigned to the parameter type 'List<Widget>'. ),
もちろん、上記のコードのように、
Dart
1 body:Row( 2 children: <Widget>[Container(color: Color(0xff2196f3),width: 48.0,height: 48.0,), 3 Container(color: Color(0xfff44336),width: 48.0,height: 48.0,)], //here2 4 ),
とすれば、何も問題なく実行されるのですが、何故ってに公式にはないbdプロパティに変わってしまい、動作できないのでしょうか?ご存知の方が入ればご教示願えれば幸いです。
よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/23 14:00