在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();
}
);
}
}
关于arrays - 为什么Flutter访问列表元素时报告错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278309/