skip to Main Content

I am used to constructing an empty List and then adding items to it.

List<String> example = [];

The index of the list is automatically created and taken care of so that I never have to think about it, but also my knowledge of how to write for the list’s index is non-existent.

The problem is that another widget requires a reference to the list’s index value whereas I have no index variable when initialising this List.

So my question is, how could I rewrite my initialisation of the empty list ‘example’ so that it refers to the list’s built-in index value for subsequent use please?

With thanks.

2

Answers


  1. Chosen as BEST ANSWER

    I saw this after diving into the underlying Dart/Flutter code for a List item:

    external factory List.generate(int length, E generator(int index),
          {bool growable = true});

    This might provide a way forward.

    If I could write:

    List<String> example = List.generate(int length, E generator(int index),
          {bool growable = true});

    If I can write the above and it amounts to exactly the same thing as the empty list 'example' of this question, then that's the answer or an example of the answer that I'm seeking.


  2. Pending the List’s object type, the answer can generally be written in the following format:

    List<String> example = List.generate(0, (index) => string_object, growable: true);

    It is possible to write an extended initialisation of an empty list in Flutter/Dart.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search