首页 > 解决方案 > Flutter 将嵌套列表与对象混淆

问题描述

这在 JS 中可以正常工作,但 Dart (Flutter) 心脏病发作了。知道为什么吗?

void main() {
  var foo = [
    {
      "some": "thing",  // comment this line and it works ??
      "test": ["one", "two", "three"]
    }
  ];

  // The operator '[]' isn't defined for the class 'Object'
  print(foo[0]["test"][0]);
}

标签: flutterdart

解决方案


请尝试如下

void main() {
  List foo = [
    {
      "some": "thing",  // comment this line and it works ??
      "test": ["one", "two", "three"]
    }
  ];

  // The operator '[]' isn't defined for the class 'Object'
  print(foo[0]["test"][0]);
}

推荐阅读