toJson method
Converts the Matrix object to a JSON-serializable Map.
This method creates a Map representation of the Matrix object that can be easily serialized to JSON. The resulting Map contains the following keys:
- 'font': The font used in the Matrix (type depends on how 'font' is defined in the class).
- 'rows': The number of rows in the Matrix.
- 'cols': The number of columns in the Matrix.
- 'data': A List of Strings, where each String represents a row in the Matrix. In these Strings, '#' represents true (or filled) cells, and '.' represents false (or empty) cells.
The 'data' field is created by transforming the 2D boolean array into a more compact string representation, where each row is converted to a string of '#' and '.' characters.
Returns:
A Map<String, dynamic> that can be serialized to JSON, representing the
current state of the Matrix object.
Implementation
Map<String, dynamic> toJson() {
return {
'font': font,
'rows': rows,
'cols': cols,
'data': _matrix.map((row) {
return List.generate(rows, (y) {
return List.generate(cols, (x) => cellGet(x, y) ? '#' : '.').join();
});
}).toList(),
};
}