add element in a list dart code example

Example 1: dart add String item to list

List<String> list = ["B", "C", "D"];
list.insert(0, "A"); // at index 0 we are adding A
// list now becomes ["A", "B", "C", "D"]

Example 2: inserting elements to list dart

import 'dart:core';

void main() {
  
  List<int> intArr = [1,2,3,4,5];
  
  //adding single int to list
  intArr.add(6);
  
  //adding multiple int to list
  intArr.addAll([7,8]);
  
  //adding element or elements to specific location
  //syntax: List.insert(index,value)
  intArr.insert(0,0);
  
  //syntax: List.insertAll(index, iterable_list_of _values)
  intArr.insertAll(0,[-2,-1]); 
  
}

Tags:

Dart Example