dart list of objects code example

Example 1: generate method o dart list

var list = new List<int>.generate(10, (i) => i + 1);

Example 2: inserting elements to integer array 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]); 
  
}

Example 3: get single element from list in dart

var firstList = [1,2,3,4,5,6]; print(firstList.firstWhere((i) => i < 4)); // 1  var sList = ['one', 'two', 'three', 'four']; print(sList.firstWhere((i) => i.length > 3)); // three

Tags:

Dart Example