skip to Main Content

    var PieceMap2 = {
      0: "a",
      1: "b",
      2: "c",
      3: "d",
      4: "e",
      5: "f",
      6: "g",
      7: "h"
    };
    List<List<String>> InitialBoard1 =
        List.filled(8, List.filled(8, "a", growable: false), growable: false);
    void main() {
      for (int i = 8; i > 0; i--) {
        for (int j = 0; j <= 7; j++) {
          String a = PieceMap2[j] ?? "null";
          String b = '$i';
          String c = a + b;
          print(c);
          InitialBoard1[i - 1][j] = c;
        }
        print(InitialBoard1[i - 1]);
      }
      for (int i = 0; i < 8; i++) {
        print(InitialBoard1[i]);
      }
    }

This is my program. Every time I’m updating InitialBoard1 but every update of a row, seemingly, overwrites previous updates. When i print it at the end, this is the output i get.

a8
b8
c8
d8
e8
f8
g8
h8
[a8, b8, c8, d8, e8, f8, g8, h8]
a7
b7
c7
d7
e7
f7
g7
h7
[a7, b7, c7, d7, e7, f7, g7, h7]
a6
b6
c6
d6
e6
f6
g6
h6
[a6, b6, c6, d6, e6, f6, g6, h6]
a5
b5
c5
d5
e5
f5
g5
h5
[a5, b5, c5, d5, e5, f5, g5, h5]
a4
b4
c4
d4
e4
f4
g4
h4
[a4, b4, c4, d4, e4, f4, g4, h4]
a3
b3
c3
d3
e3
f3
g3
h3
[a3, b3, c3, d3, e3, f3, g3, h3]
a2
b2
c2
d2
e2
f2
g2
h2
[a2, b2, c2, d2, e2, f2, g2, h2]
a1
b1
c1
d1
e1
f1
g1
h1
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]

I was expecting to get the final updated value of InitialBoard1 as:

[a1, b1, c1, d1, e1, f1, g1, h1]
.
.
.
[a6, b6, c6, d6, e6, f6, g6, h6]
[a7, b7, c7, d7, e7, f7, g7, h7]
[a8, b8, c8, d8, e8, f8, g8, h8]

Any help as to why i’m not getting the expected result would be appreciated. Thank you.

2

Answers


  1. When you use List.filled it fills the list with the same value multiple times. In your case you end up with 8 references to the same array. When you print each reference at the end, they are of course the same.

    Instead use List.generate. This will generate the list by calling the function 8 times, creating 8 different copies.

      final InitialBoard1 = List.generate(
        8,
        (_) => List.filled(8, 'a', growable: false),
      );
    
    Login or Signup to reply.
  2. Try the following code to get your desired result:

    Map<int, String> PieceMap2 = {
      0: "a",
      1: "b",
      2: "c",
      3: "d",
      4: "e",
      5: "f",
      6: "g",
      7: "h"
    };
    List<List<String>> InitialBoard1 = [];
    void main() {
      for (int i = 0; i < PieceMap2.length; i++) {
        List<String> list = [];
        for (int j = 0; j < PieceMap2.length; j++) {
          list.add("${PieceMap2[j]}${(i + 1).toString()}");
        }
        InitialBoard1.add(list);
      }
      for (int i = 0; i < InitialBoard1.length; i++) {
        print(InitialBoard1[i]);
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search