From 81f0be949be3faa41012eaf242e27e70a668334b Mon Sep 17 00:00:00 2001 From: sunboyy Date: Thu, 20 Jun 2024 22:41:15 +0700 Subject: [PATCH] Refactor avoid late keyword as much as possible --- lib/src/excel.dart | 71 +++++++++-------------- lib/src/parser/parse.dart | 13 ++--- lib/src/save/save_file.dart | 12 ++-- lib/src/sharedStrings/shared_strings.dart | 7 +-- lib/src/sheet/border_style.dart | 29 +++++---- lib/src/sheet/sheet.dart | 28 ++++----- test/excel_test.dart | 2 +- 7 files changed, 64 insertions(+), 98 deletions(-) diff --git a/lib/src/excel.dart b/lib/src/excel.dart index 504af98a..f57588d7 100644 --- a/lib/src/excel.dart +++ b/lib/src/excel.dart @@ -23,31 +23,31 @@ Excel _newExcel(Archive archive) { /// Decode a excel file. class Excel { - late bool _styleChanges; - late bool _mergeChanges; - late bool _rtlChanges; - - late Archive _archive; - - late Map _sheets; - late Map _xmlFiles; - late Map _xmlSheetId; - late Map> _cellStyleReferenced; - late Map _sheetMap; - - late List _cellStyleList; - late List _patternFill; - late List _mergeChangeLook; - late List _rtlChangeLook; - late List<_FontStyle> _fontStyleList; - late List _numFmtIds; - late NumFormatMaintainer _numFormats = NumFormatMaintainer(); - late List<_BorderSet> _borderSetList; - - late _SharedStringsMaintainer _sharedStrings; - - late String _stylesTarget; - late String _sharedStringsTarget; + bool _styleChanges = false; + bool _mergeChanges = false; + bool _rtlChanges = false; + + Archive _archive; + + final Map _sheets = {}; + final Map _xmlFiles = {}; + final Map _xmlSheetId = {}; + final Map> _cellStyleReferenced = {}; + final Map _sheetMap = {}; + + List _cellStyleList = []; + List _patternFill = []; + final List _mergeChangeLook = []; + final List _rtlChangeLook = []; + List<_FontStyle> _fontStyleList = []; + final List _numFmtIds = []; + final NumFormatMaintainer _numFormats = NumFormatMaintainer(); + List<_BorderSet> _borderSetList = []; + + _SharedStringsMaintainer _sharedStrings = _SharedStringsMaintainer._(); + + String _stylesTarget = ''; + String _sharedStringsTarget = ''; String get _absSharedStringsTarget { if (_sharedStringsTarget.isNotEmpty && _sharedStringsTarget[0] == "/") { return _sharedStringsTarget.substring(1); @@ -58,26 +58,7 @@ class Excel { String? _defaultSheet; late Parser parser; - Excel._(Archive archive) { - _styleChanges = false; - _mergeChanges = false; - _rtlChanges = false; - _sheets = {}; - _xmlFiles = {}; - _xmlSheetId = {}; - _cellStyleReferenced = >{}; - _sheetMap = {}; - _cellStyleList = []; - _patternFill = []; - _mergeChangeLook = []; - _rtlChangeLook = []; - _fontStyleList = <_FontStyle>[]; - _numFmtIds = []; - _stylesTarget = ''; - _sharedStringsTarget = ''; - - _archive = archive; - _sharedStrings = _SharedStringsMaintainer._(); + Excel._(this._archive) { parser = Parser._(this); parser._startParsing(); } diff --git a/lib/src/parser/parse.dart b/lib/src/parser/parse.dart index 0fefe5ba..6eee9510 100644 --- a/lib/src/parser/parse.dart +++ b/lib/src/parser/parse.dart @@ -1,14 +1,11 @@ part of excel; class Parser { - late Excel _excel; - late List _rId; - late Map _worksheetTargets; - Parser._(Excel excel) { - this._excel = excel; - this._rId = []; - this._worksheetTargets = {}; - } + final Excel _excel; + final List _rId = []; + final Map _worksheetTargets = {}; + + Parser._(this._excel); void _startParsing() { _putContentXml(); diff --git a/lib/src/save/save_file.dart b/lib/src/save/save_file.dart index b6df9dec..b90543c4 100644 --- a/lib/src/save/save_file.dart +++ b/lib/src/save/save_file.dart @@ -2,13 +2,11 @@ part of excel; class Save { final Excel _excel; - late Map _archiveFiles; - late List _innerCellStyle; + final Map _archiveFiles = {}; + final List _innerCellStyle = []; final Parser parser; - Save._(this._excel, this.parser) { - _archiveFiles = {}; - _innerCellStyle = []; - } + + Save._(this._excel, this.parser); void _addNewColumn(XmlElement columns, int min, int max, double width) { columns.children.add(XmlElement(XmlName('col'), [ @@ -172,7 +170,7 @@ class Save { /// Writing Font Color in [xl/styles.xml] from the Cells of the sheets. void _processStylesFile() { - _innerCellStyle = []; + _innerCellStyle.clear(); List innerPatternFill = []; List<_FontStyle> innerFontStyle = <_FontStyle>[]; List<_BorderSet> innerBorderSet = <_BorderSet>[]; diff --git a/lib/src/sharedStrings/shared_strings.dart b/lib/src/sharedStrings/shared_strings.dart index 1e5614ae..453266de 100644 --- a/lib/src/sharedStrings/shared_strings.dart +++ b/lib/src/sharedStrings/shared_strings.dart @@ -55,10 +55,9 @@ class _SharedStringsMaintainer { class _IndexingHolder { final int index; - late int count; - _IndexingHolder(this.index, [int _count = 1]) { - this.count = _count; - } + int count; + + _IndexingHolder(this.index, [int _count = 1]) : count = _count; void increaseCount() { this.count += 1; diff --git a/lib/src/sheet/border_style.dart b/lib/src/sheet/border_style.dart index 0e934f65..c4773989 100644 --- a/lib/src/sheet/border_style.dart +++ b/lib/src/sheet/border_style.dart @@ -1,15 +1,14 @@ part of excel; class Border extends Equatable { - late final BorderStyle? borderStyle; - late final String? borderColorHex; + final BorderStyle? borderStyle; + final String? borderColorHex; - Border({BorderStyle? borderStyle, ExcelColor? borderColorHex}) { - this.borderStyle = borderStyle == BorderStyle.None ? null : borderStyle; - this.borderColorHex = borderColorHex != null - ? _isColorAppropriate(borderColorHex.colorHex) - : null; - } + Border({BorderStyle? borderStyle, ExcelColor? borderColorHex}) + : borderStyle = borderStyle == BorderStyle.None ? null : borderStyle, + borderColorHex = borderColorHex != null + ? _isColorAppropriate(borderColorHex.colorHex) + : null; @override String toString() { @@ -24,13 +23,13 @@ class Border extends Equatable { } class _BorderSet extends Equatable { - late final Border leftBorder; - late final Border rightBorder; - late final Border topBorder; - late final Border bottomBorder; - late final Border diagonalBorder; - late final bool diagonalBorderUp; - late final bool diagonalBorderDown; + final Border leftBorder; + final Border rightBorder; + final Border topBorder; + final Border bottomBorder; + final Border diagonalBorder; + final bool diagonalBorderUp; + final bool diagonalBorderDown; _BorderSet({ required this.leftBorder, diff --git a/lib/src/sheet/sheet.dart b/lib/src/sheet/sheet.dart index 014be465..cf0249cc 100644 --- a/lib/src/sheet/sheet.dart +++ b/lib/src/sheet/sheet.dart @@ -1,20 +1,20 @@ part of excel; class Sheet { - late Excel _excel; - late String _sheet; - late bool _isRTL; - late int _maxRows; - late int _maxColumns; + final Excel _excel; + final String _sheet; + bool _isRTL = false; + int _maxRows = 0; + int _maxColumns = 0; double? _defaultColumnWidth; double? _defaultRowHeight; Map _columnWidths = {}; Map _rowHeights = {}; Map _columnAutoFit = {}; - late FastList _spannedItems; - late List<_Span?> _spanList; - late Map> _sheetData; - late HeaderFooter? _headerFooter; + FastList _spannedItems = FastList(); + List<_Span?> _spanList = []; + Map> _sheetData = {}; + HeaderFooter? _headerFooter; /// /// It will clone the object by changing the `this` reference of previous oldSheetObject and putting `new this` reference, with copying the values too @@ -32,7 +32,7 @@ class Sheet { isRTLVal: oldSheetObject._isRTL, headerFooter: oldSheetObject._headerFooter); - Sheet._(Excel excel, String sheetName, + Sheet._(this._excel, this._sheet, {Map>? sh, List<_Span?>? spanL_, FastList? spanI_, @@ -43,14 +43,6 @@ class Sheet { Map? rowHeightsVal, Map? columnAutoFitVal, HeaderFooter? headerFooter}) { - _excel = excel; - _sheet = sheetName; - _sheetData = >{}; - _spanList = <_Span?>[]; - _spannedItems = FastList(); - _isRTL = false; - _maxRows = 0; - _maxColumns = 0; _headerFooter = headerFooter; if (spanL_ != null) { diff --git a/test/excel_test.dart b/test/excel_test.dart index f94c4a63..1e0a27a7 100644 --- a/test/excel_test.dart +++ b/test/excel_test.dart @@ -558,7 +558,7 @@ void main() { var file = './test/test_resources/example.xlsx'; var bytes = File(file).readAsBytesSync(); var excel = Excel.decodeBytes(bytes); - Sheet? sheetObject = excel.tables['Sheet1']!; + Sheet sheetObject = excel.tables['Sheet1']!; sheetObject.headerFooter!.oddHeader = "Foo"; sheetObject.headerFooter!.oddFooter = "Bar";