I have a RowComponent in Flutter that consists of three child components (header, body, and image). Each of these child components needs to have a function to convert itself to JSON. To achieve this, I created a mixin named JsonConvertible:
mixin JsonConvertible {
Map<String, dynamic> convertToJson();
}
Now, in my RowComponent, I want to implement the convertToJson function while ensuring that the RowComponent remains const. The desired structure of the JSON output should include data from each child component, for example:
@override
Map<String, dynamic> convertToJson() {
return {
"widget_type": "row",
"data": null,
"children": [
{"widget_type": "header", "data": header.data, "children": null},
{"widget_type": "body", "data": body.data, "children": null},
{"widget_type": "image", "data": image.data, "children": null}
]
};
}
My attempts to achieve this by assigning each child component to a variable with late and passing the widgets to the cubit have resulted in the RowComponent becoming non-const or facing difficulties accessing the widgets. Is there a solution that allows me to access the three child components inside convertToJson without affecting the const status of RowComponent?