skip to Main Content

I am using VS code
when I create List without assigning values …the compiler throw Error: Couldn’t find constructor ‘List’:
enter image description here

I tried to declare fixed length List but error occurs .please one can fix this I am beginner.

2

Answers


  1. It’s not the correct syntax for the dart.

    Here is the way you do this:

    void main() {
      List<int?> lst = List<int?>.filled(3, null);
      lst[0]=1;
      lst[1]=2;
      lst[2]=3;
      print(lst.runtimeType);  
      print(lst);
    }
    

    For more read documentation.

    Login or Signup to reply.
  2. you implement wrong syntax in dart so correct syntax way is.

    void main() {
      List<int> list =  [];
      list.add(11);
      list.add(12);
      list.add(13);
      print('list: $list');
    }
    
    o/p:  list:  [11, 12, 13]
    

    2nd way is

    List mList = List.filled(5, null, growable: false);
    mList[0] = 12;
    mList[1] = 22;
    mList[2] = 33 ;
    print(mList)
    
    o/p: [12, 22, 33, null, null]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search