How can i add an entry to a specific index in a list?

前端 未结 4 846
天涯浪人
天涯浪人 2021-01-11 13:09

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.

相关标签:
4条回答
  • 2021-01-11 13:46

    You can use the insert() and removeAt() methods now.

    0 讨论(0)
  • 2021-01-11 13:53

    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));
    

    You can try it out on the DartBoard here

    0 讨论(0)
  • 2021-01-11 13:57

    Dart 2

    var idx = 3;
    list.insert(idx, 'foo');
    

    Depends on whether you want to insert a single item or a bunch of items

    • https://api.dartlang.org/stable/2.1.0/dart-core/List/insert.html
    • https://api.dartlang.org/stable/2.1.0/dart-core/List/insertAll.html

    All available methods https://api.dartlang.org/stable/2.1.0/dart-core/List-class.html#instance-methods

    0 讨论(0)
  • 2021-01-11 14:02

    Ok, it seams as if list.insertRange(index, range, [elem]) is what i am looking for.

    0 讨论(0)
提交回复
热议问题