1

In a flutter app, I have a List of 25 items (for example int numbers) which I want to be displayed in a table of 5 rows & 5 columns. I want to use the stream API (map, take, etc.) to build each item using its own index (like in ListView.builder).

I imagine something like: Table(children: List.generate(25, (i) => i).map((i) => MyTableCell(i)).toList().take(5, into(TableRow()))) but of course this won't do...

I really don't want to use for loops for that purpose . Any ideas?

I hope that I explained myself well. I'll add details if needed. Thank you.

idow09
  • 504
  • 1
  • 5
  • 15

2 Answers2

1

A GridView takes a cross-axis count, and a stream of items. No math required.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • Of course GridView will be awesome, but then I saw https://youtu.be/_lbE0wsVZSw which states that Table is preferred over GridView when you want a non-scrollable widget. – idow09 Nov 22 '18 at 16:52
1

I agree that using a GridView would be easiest. If your really want to use a Table, I guess the challenge is dividing your List<int> of 25 items into a List<List<int>> so that you can use it as children for the Table?

I'd suggest:

yourList.fold([[]], (list, x) {    
  return list.last.length == 5 ? (list..add([x])) : (list..last.add(x));
}); 

(Solution shamelessly copied from this post - because it's a great one :-)).

However, some might say that a for loop doing the same thing would be more common and thus easier to read for many... ;-)

rgisi
  • 858
  • 6
  • 21