skip to Main Content

I want to create a list with a specified index at the beginning, how about a case like this :

from start a list => List data = [1,2,3,4,5];
to new list newData = [3,4,5];

and how to make the index start from 2, in list newData!

2

Answers


  1. List<int> data = [1,2,3,4,5,6];
    List<int> newData = data.getRange(2, data.length).toList();
    print("$newData");
    //output : I/flutter (15236): [3, 4, 5, 6]
    
    Login or Signup to reply.
  2. you can use skip method from list.

    its more safe instead of getRange, because it will not throw error index range if the list are empty.

    or you can also combine with take() mehtod.

      List a = [1,2,4,5,6,8,9];
      
      print(a.skip(3);// [5, 6, 8, 9]
      print(a.take(3)); // [1, 2, 4]
      print (a.skip(4).take(2)); // [6, 8]
      
    
      // this will not throw error index range
      print(a.skip(100)); // []
      
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search