如何将条目添加到列表中的特定索引?
问题描述:
我可以调用list.add(),它会在末尾添加,但是没有方便的方法可以将条目添加到特定索引中,同时又可以增加列表。
I can call list.add() which adds at the end but there is no convenient way to add an entry to a specific index which at the same time grows the list.
答
您可以使用 insertRange ,它将在添加新元素时增加列表。
You can use insertRange, it will grow the list when adding new elements.
var list = ["1","3","4"];
list.insertRange(0, 1, "0");
list.insertRange(2, 1, "2");
list.forEach((e) => print(e));
您可以在DartBoard上试用此处
You can try it out on the DartBoard here