在Dart语言中声明列表的问题。

huangapple go评论141阅读模式
英文:

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':
在Dart语言中声明列表的问题。

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':
在Dart语言中声明列表的问题。

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&lt;int?&gt; lst = List&lt;int?&gt;.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&lt;int&gt; list =  [];
  list.add(11);
  list.add(12);
  list.add(13);
  print(&#39;list: $list&#39;);
}

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]

huangapple
  • 本文由 发表于 2023年7月6日 22:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定