如何从飞镖中的变量列表中获取值

How to get values from list in variables in dart

我在Python中看到了以下内容:

my_list = [1, 2, 3, 4]
first_number, *other_numbers, last_number = my_list

# here first_number becomes 1, other_numbers becomes [2, 3] and last_number becomes 4

如何在具有相同值的飞镖中使用 *

我试过了:

void main () {
  List<int> my_list = [1, 2, 3, 4];
  var first_number, *other_numbers, last_number = my_list;
}

但是上面是语法错误

解决该问题的一种方法是使用 List.getRange();

你的代码会变成这样:


void main () {
  List<int> my_list = [1, 2, 3, 4];
  var first_number = my_list[0], other_numbers = my_list.getRange(1,3), last_number = my_list[3]; 
  print(first_number);
  print(other_numbers);
  print(last_number);
}