英文:
Issue on Declaration of List in Dart language
问题
I am using VS code
when I create List without assigning values ...the compiler throw Error: Couldn't find constructor 'List':
I tried to declare fixed length List but error occurs .please one can fix this I am beginner.
英文:
I am using VS code
when I create List without assigning values ...the compiler throw Error: Couldn't find constructor 'List':
I tried to declare fixed length List but error occurs .please one can fix this I am beginner.
答案1
得分: 2
这不是Dart的正确语法。
以下是正确的做法:
void main() {
List<int?> lst = List<int?>.filled(3, null);
lst[0] = 1;
lst[1] = 2;
lst[2] = 3;
print(lst.runtimeType);
print(lst);
}
更多信息,请参阅文档。
英文:
It's not the correct syntax for the dart.
Here is the way you do this:
void main() {
List<int?> lst = List<int?>.filled(3, null);
lst[0]=1;
lst[1]=2;
lst[2]=3;
print(lst.runtimeType);
print(lst);
}
For more read documentation.
答案2
得分: 1
以下是已翻译的代码部分:
你在Dart中实现了错误的语法,所以正确的语法方式是。
void main() {
List<int> list = [];
list.add(11);
list.add(12);
list.add(13);
print('list: $list');
}
输出结果: list: [11, 12, 13]
第二种方式是
List mList = List.filled(5, null, growable: false);
mList[0] = 12;
mList[1] = 22;
mList[2] = 33 ;
print(mList)
输出结果: [12, 22, 33, null, null]
英文:
you implement wrong syntax in dart so correct syntax way is.
void main() {
List<int> list = [];
list.add(11);
list.add(12);
list.add(13);
print('list: $list');
}
o/p: list: [11, 12, 13]
2nd way is
List mList = List.filled(5, null, growable: false);
mList[0] = 12;
mList[1] = 22;
mList[2] = 33 ;
print(mList)
o/p: [12, 22, 33, null, null]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论