PlutoRow.fromJson constructor
Create PlutoRow in json type. The key of the json you want to generate must match the key of PlutoColumn.
final json = {
'column1': 'value1',
'column2': 'value2',
'column3': 'value3',
};
final row = PlutoRow.fromJson(json);
If you want to create a group row with children, you need to pass childrenField
.
// Example when the child row field is children
final json = {
'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',
},
],
};
final rowGroup = PlutoRow.fromJson(json, childrenField: 'children');
Implementation
factory PlutoRow.fromJson(
Map<String, dynamic> json, {
String? childrenField,
}) {
final Map<String, PlutoCell> cells = {};
final bool hasChildren =
childrenField != null && json.containsKey(childrenField);
final entries = hasChildren
? json.entries.where((e) => e.key != childrenField)
: json.entries;
assert(!hasChildren || json.length - 1 == entries.length);
for (final item in entries) {
cells[item.key] = PlutoCell(value: item.value);
}
PlutoRowType? type;
if (hasChildren) {
assert(json[childrenField] is List<Map<String, dynamic>>);
final children = <PlutoRow>[];
for (final child in json[childrenField]) {
children.add(PlutoRow.fromJson(child, childrenField: childrenField));
}
type = PlutoRowType.group(children: FilteredList(initialList: children));
}
return PlutoRow(cells: cells, type: type);
}