flutterarray-mergedatacolumn

In Flutter how to combine two map'ed variables


I am new to Flutter. I am sending two separate strings from Json to create a table in Flutter:

String one contains Column Titles with ~ separator

"column_titles": "Col Title 1~ColTitle-2~Title3~Column Title 4"

And the other string stores if the value is Numeric or Alpha (can be other formats but for simplicity A or N for the time being)

"column_types": "A~N~N~N"

I am converting the above into separate lists as follows:

var columnTypes = widget.columnTypes
    .split('~')
    .map((String text) => text)
    .toList();

var columnTitles = widget.titles
    .split('~')
    .map((String text) => DataColumn(
        label: Text(text), numeric: true)) // decide true or false based on columnTypes
    .toList();

Based on the value of text in columnTypes I need to insert true or false for numeric parameter of DataColumn in columnTitles

I have searched but couldn't find a good example.


Solution

  • You could use collection for to build the DataColumn list.

    final columnTitles = widget.titles.split('~');
    final columnTypes = widget.columnTypes.split('~');
    
    final dataColumns = [
      for (var i = 0; i < columnTitles.length; i++)
        DataColumn(
          label: Text(columnTitles[i]),
          numeric: columnTypes[i] == 'N',
        ),
    ];