skip to Main Content

So I have a situation where I have a parent list, and inside it has two more lists. So basically [[],[]].
Now, if I update someList[0][0] = ‘A’ automatically someList[1][0] updates.

Thankyou in advanced.

2

Answers


  1. You’ve basically provided no info, but if you add the same list to the parent list, then do an update to the inner lists, both will update "automatically", because it is the same list.. Variables store references.

    If you don’t want that to happen, you need to make a copy of the list.

    See this example:

    https://dartpad.dev/?id=7104e40df1cfde463c32ff5ef264afaa

    Login or Signup to reply.
  2. This below extension method might helpful for you:

    extension ListExtensions on List<List> {
      List addItemToAllMembers(int indexOfSubList, var item ,) {
        for (int i = 0; i < this.length; i++) {
          this[i][indexOfSubList] = item;
        }
        return this;
      }
    }
    

    Note: I just created it for only List<int>; you can modify it as per your needs.

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