【问题标题】:List.generate index null " only at Textstyle, color: " flutterList.generate index null " 仅在 Textstyle, color: " 颤动
【发布时间】: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


【解决方案1】:

看起来index 不知何故是错误的。打印出index 值,看看btncolor 是否包含这样的值。由于缺少完整代码,很难说具体出了什么问题。

【讨论】:

  • 我犯了一个没有从0开始的菜鸟错误,你指点我谢谢!
猜你喜欢
  • 2020-10-08
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 2020-11-19
  • 2020-05-26
  • 1970-01-01
  • 2021-06-27
  • 2021-07-01
相关资源
最近更新 更多