0

I have a 1d list of integers like:

List<int> x = [1, 4, 2, 8, 9, 3, 6, 5, 7];

I want to convert this list to a 2d list like this:

List<List<int>> y = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
Rasheeq Zaman
  • 29
  • 1
  • 4
  • 2
    Does this answer your question? [How do I split or chunk a list into equal parts, with Dart?](https://stackoverflow.com/questions/22274033/how-do-i-split-or-chunk-a-list-into-equal-parts-with-dart) – esentis Feb 10 '22 at 09:06
  • Is your 1d list supposed to be `[1, 4, 7, 2, 5, 8, 3, 6, 9]` instead? If not, then by what logic do you expect it to be partitioned into sublists? – jamesdlin Feb 10 '22 at 09:23
  • use [splitBeforeIndexed](https://api.flutter.dev/flutter/package-collection_collection/IterableExtension/splitBeforeIndexed.html) - something like: `final y = x.splitBeforeIndexed((i, v) => i % 3 == 0).toList();` – pskink Feb 10 '22 at 15:14

1 Answers1

0
List<int> x = [1, 4, 2, 8, 9, 3, 6, 5, 7];

// make ascending list 

List<int> x = (b, a) => a.compareTo(b);

// install package matrix 2d dart package link : //https://pub.dev/packages/matrix2d

list = X.reshape(3,3); 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103