![](/img/trans.png)
[英]Why does accessing an array element throw an array-as-string error in PHP?
[英]Why does Flutter report error when accessing list element?
在 Flutter (Dart) 中创建列表时,我收到来自 Android Studio 的语法错误。
即使是从 Flutter 文档中复制的最简单的形式,我也会遇到同样的错误。
编码:
var simonSequence = new List<int>(3);
var c = simonSequence[0]; //error here
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; //also error here
在访问列表元素的行上给出错误。
任何建议表示赞赏。
因为您在类范围内编写代码,而您必须在函数中编写它。
这就是你在做什么
class _SimonState extends State<Simon>{
//other codes
var simonSequence = new List<int>(3);
var c = simonSequence[0]; //error
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; //error
}
这就是你的代码应该是什么样子
class _SimonState extends State<Simon>{
//other codes
//some function you want your code to be called from
void anyFunction(){
var simonSequence = new List<int>(3);
var c = simonSequence[0]; //error
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; //error
}
@override
Widget build(BuildContext context) {
//then you will call your function anywhere you need like here
//for example
return RaisedButton(
onPressed:(){
anyFunction();
}
);
}
}
这仅仅是因为您试图访问类范围内的声明变量。 它被标记为错误,因为它不是变量的声明。 有关详细信息,请参阅以下代码及其注释:
class _SimonState extends State<Simon>{
// Here you can only declare your variables.
// Or declaring a method.
var simonSequence = new List<int>(3);
var c = simonSequence[0]; // Error! This is not a variable declaration.
final anEmptyListOfDouble = <int>[];
anEmptyListOfDouble[0]=0; // Error! This is not a variable declaration.
...
void anotherMethod() {
...
// Correct, your accessing the variable here.
var c = simonSequence[0];
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.