1

I want my DataTable to do exactly what the ListView.builder does. This build method iterates an index and also will take my object while for the life of me I can't figure this out for the DataTable in my situation. As evidence here and here.

My ultimate goal is to use my Hive box to dynamically add data to DataTable rows using an index iteration.

So I discovered that the build method can be referenced and accessed, and this is the exact functionality I need and want. This (indexing) should ABSOLUTELY be a standard optional property for DataTable, but it's not.

/// Signature for a function that creates a widget for a given index, e.g., in a
/// list.
///
/// Used by [ListView.builder] and other APIs that use lazily-generated widgets.
///
/// See also:
///
///  * [WidgetBuilder], which is similar but only takes a [BuildContext].
///  * [TransitionBuilder], which is similar but also takes a child.
typedef IndexedWidgetBuilder = Widget Function(BuildContext context, int index);

Is it possible to implement this so it can be used in a function that returns a DataTable? How can this be done?

Help MUCH MUCH appreciated! Racking my braains over this.

RobbB
  • 1,214
  • 11
  • 39
  • I don't really understand your question. Can you maybe post some code showcasing your problem? I just read your other two questions because I thought I might be missing context, but I'm not entirely sure what they are asking either. All 3 questions have 0 answers, indicating others may feel the same way. – nvoigt Mar 03 '21 at 06:57
  • Thanks for the input on that. That explains why I did not get any answers. But below answer answers it anyway. – RobbB Mar 04 '21 at 00:17

1 Answers1

2

DataTable can't be done "incrementally" like a ListView.builder, because all the cells need to be laid out to get their intrinsic sizes to know how to create the overall size for the rows and columns.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • 1
    Damn! That sucks, but it's a good answer and explains a lot. And I just figured out about 50 ways not to do it hehe. Thanks for the input Randal. – RobbB Mar 04 '21 at 00:17
  • This calls for a better and more applicable question: https://stackoverflow.com/questions/66467109/how-do-i-use-hive-and-datatable-together-and-keep-code-simple-in-flutter-dart – RobbB Mar 04 '21 at 01:01
  • I was just going over my old attempt again and I would like to come back at you for another question @RandalSchwartz. if I'm calling a method for building the List of DataCells and iterating before the DataCell should that not work? Because it makes sense that it would according to examples I've seen where data is iterated over an index and loaded dynamically like here: https://www.appsdeveloperblog.com/datatable-in-flutter-explained/ – RobbB Mar 04 '21 at 03:05
  • 1
    Sorry Randal, I recently SUCCESSFULLY accomplished exactly what I was attempting and asked here. Your answer is incorrect and false. Rows has a .generate() method which in actual fact DOES incrementally build and also DOES in fact build and re-build. Sorry for my over-enthusiasm but I'm a green green noob so I'm quite proud of what I just solved! See my answer-- https://stackoverflow.com/questions/66502511/how-to-dynamically-load-data-to-datatable-in-a-simple-fashion-with-hive-box-in/66502512#66502512 – RobbB Mar 06 '21 at 04:53
  • I don't see a .generate() method anywhere in a DataTable. Can you provide a specific link? – Randal Schwartz Mar 06 '21 at 18:15
  • I found this: https://api.flutter.dev/flutter/dart-core/List/List.generate.html I guess its a method of List – RobbB Mar 06 '21 at 22:40
  • That .generate just makes your rows variable length, but does nothing for an incremental build like ListView.builder does. Definitely not the same thing. – Randal Schwartz Mar 06 '21 at 22:42
  • Look I'm not experienced enough to know the fine details here. All I know is in my answer here: https://stackoverflow.com/a/66502512/13945484 I used the .generate() method to build by index and it worked perfectly and also is rebuildable... My main point here is your answer states that DataTable is not buildable "incrementally" which in fact is not true – RobbB Mar 06 '21 at 22:47
  • 1
    It is *not* building it incrementally. You are handing it *all* the rows at once -- when you get "14" for the row number, you are returning rows for 0 to 14... rebuilding them for each new row. That's not how ListView.builder works. – Randal Schwartz Mar 06 '21 at 22:51
  • I think we had a misunderstanding along the way here. I had difficulty communicating accurately what I wanted above concerning the DataTable above. This is good info to have, thanks. – RobbB Mar 06 '21 at 23:07
  • See my comment on your solution at your other post, about just using .map – Randal Schwartz Mar 06 '21 at 23:11