toJson method

Map<String, dynamic> toJson({
  1. bool includeChildren = true,
  2. String childrenField = 'children',
})

Convert the row to json type.

// Assuming you have a line like below.
final PlutoRow row = PlutoRow(cells: {
  'column1': PlutoCell(value: 'value1'),
  'column2': PlutoCell(value: 'value2'),
  'column3': PlutoCell(value: 'value3'),
});

final json = row.toJson();
// toJson is returned as below.
// {
//   'column1': 'value1',
//   'column2': 'value2',
//   'column3': 'value3',
// }

In case of group row, includeChildren is true (default) If childrenField is set to 'children' (default), the following is returned.

// Assuming you have rows grouped by 1 depth.
final PlutoRow row = PlutoRow(
  cells: {
    'column1': PlutoCell(value: 'group value1'),
    'column2': PlutoCell(value: 'group value2'),
    'column3': PlutoCell(value: 'group value3'),
  },
  type: PlutoRowType.group(
    children: FilteredList(initialList: [
      PlutoRow(
        cells: {
          'column1': PlutoCell(value: 'child1 value1'),
          'column2': PlutoCell(value: 'child1 value2'),
          'column3': PlutoCell(value: 'child1 value3'),
        },
      ),
      PlutoRow(
        cells: {
          'column1': PlutoCell(value: 'child2 value1'),
          'column2': PlutoCell(value: 'child2 value2'),
          'column3': PlutoCell(value: 'child2 value3'),
        },
      ),
    ]),
  ),
);

final json = row.toJson();
// It is returned in the following format.
// {
//   'column1': 'group value1',
//   'column2': 'group value2',
//   'column3': 'group value3',
//   'children': [
//     {
//       'column1': 'child1 value1',
//       'column2': 'child1 value2',
//       'column3': 'child1 value3',
//     },
//     {
//       'column1': 'child2 value1',
//       'column2': 'child2 value2',
//       'column3': 'child2 value3',
//     },
//   ],
// }

Implementation

Map<String, dynamic> toJson({
  bool includeChildren = true,
  String childrenField = 'children',
}) {
  final json = cells.map((key, value) => MapEntry(key, value.value));

  if (!includeChildren || !type.isGroup) return json;

  final List<Map<String, dynamic>> children = type.group.children
      .map(
        (e) => e.toJson(childrenField: childrenField),
      )
      .toList();

  json[childrenField] = children;

  return json;
}