Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error about dart's two-dimensional array?

void main() {
  int pid = 12;
  int data1 = [12][1];
  for (int i = 0; i < pid; i++) {
    data1[i][0] = 0; // This is wrong
  }
}

Error message: The operator ‘[]’ isn’t defined for the type ‘int’.Try defining the operator ‘[]’
// This is not wrong in other languages,Please help answer thank you.
//Why doesn’t this expression work? How to write it correctly

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the code with correct syntax.

void main() {
  int pid = 12;
  List<List<int>> data1 = List.generate(pid, (_) => [0, 0]); // Initialize a 2D list

  for (int i = 0; i < pid; i++) {
    data1[i][0] = 0; // Assign 0 to the first element of each sublist
  }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading