[英]List.generate index null “ only at Textstyle, color: ” flutter
我有一個 map 地圖,每個地圖存儲 4 個 colors 並像這樣繼續,從 1 到 20;
Map btncolor = {
"1": {
"a": Colors.grey[800],
"b": Colors.grey[800],
"c": Colors.grey[800],
"d": Colors.grey[800],
},
"2": {
"a": Colors.grey[300],
"b": Colors.grey[300],
"c": Colors.grey[300],
"d": Colors.grey[300],
}, [...]
要在我的小部件 TextStyle 中訪問它,我正在使用color: btncolor[index][option] ,其中選項是“a”、“b”、“c”、“d”,而 int索引來自 List.generate:
我可以在任何地方使用我的小部件中的索引來查找 arrays 中的位置並且工作正常,但是如果我嘗試將它與btncolor map 一起使用,它會給我 null,
在 null 上調用了方法“[]”。 接收方:null 嘗試調用:[ ] (''a'')
順便說一句,嘗試顏色: btncolor["1"][option]工作正常。
我不確定我們是否需要這里的小部件代碼,如果需要,請告訴我,我會編輯。 [編輯]
Widget choicetab(String option){
var screen = MediaQuery.of(context).size;
var wid = screen.width;
var hei = screen.height;
return AbsorbPointer(
absorbing: false,
child: MaterialButton(
padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
height: hei/20,
child: Container(
width: wid,
// color: Colors.red,
child: Text(
option+") "+mydata[1][random_array[index].toString()][option],
style: TextStyle(
fontSize: 15.0,
Error here ---> color: btncolor[index.toString()][option]. <---
),
textAlign: TextAlign.start,
),
),
onPressed: () => checkanswer(option),
),
);
}
看起來index
在某種程度上是錯誤的。 打印出index
值,看看btncolor
是否包含這樣的值。 由於缺乏完整的代碼,很難說具體出了什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.