【发布时间】:2020-07-25 18:40:28
【问题描述】:
我有一张地图,每个地图存储 4 种颜色,并像这样继续,从 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 index 来自 List.generate:
我可以在任何地方使用我的小部件中的索引来查找数组中的位置并且工作正常,但是如果我尝试将它与 btncolor 地图一起使用,它会给我 null,
在 null 上调用了方法“[]”。接收者:null 尝试调用:[ ] (''a'')
btw 尝试 color: 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),
),
);
}
【问题讨论】:
-
你可以试试颜色:btncolor["$index"][option]
-
相同的错误日志。
标签: arrays android-studio flutter dart colors