From b14c1dc9dc2c60ffff68d01430e009ab745177df Mon Sep 17 00:00:00 2001 From: Miller Adulu Date: Sat, 13 Mar 2021 14:16:32 +0300 Subject: [PATCH] Remove unnecessary checks --- lib/models/documents/nodes/embed.dart | 3 +-- lib/models/documents/nodes/leaf.dart | 4 +-- lib/models/documents/nodes/node.dart | 3 --- lib/models/quill_delta.dart | 9 +++---- lib/models/rules/insert.dart | 4 +-- lib/models/rules/rule.dart | 2 -- lib/widgets/controller.dart | 13 +++------ lib/widgets/default_styles.dart | 3 +-- lib/widgets/delegate.dart | 2 +- lib/widgets/editor.dart | 38 +++------------------------ lib/widgets/keyboard_listener.dart | 4 +-- lib/widgets/raw_editor.dart | 2 -- lib/widgets/text_block.dart | 21 +++------------ lib/widgets/text_selection.dart | 23 +++++----------- lib/widgets/toolbar.dart | 33 +++++------------------ 15 files changed, 33 insertions(+), 131 deletions(-) diff --git a/lib/models/documents/nodes/embed.dart b/lib/models/documents/nodes/embed.dart index a8dff833..ee8dfbed 100644 --- a/lib/models/documents/nodes/embed.dart +++ b/lib/models/documents/nodes/embed.dart @@ -3,8 +3,7 @@ class Embeddable { final dynamic data; Embeddable(this.type, this.data) - : assert(type != null), - assert(data != null); + ; Map toJson() { Map m = {type: data}; diff --git a/lib/models/documents/nodes/leaf.dart b/lib/models/documents/nodes/leaf.dart index 57a624c8..b5c41f6b 100644 --- a/lib/models/documents/nodes/leaf.dart +++ b/lib/models/documents/nodes/leaf.dart @@ -27,7 +27,7 @@ abstract class Leaf extends Node { @override void applyStyle(Style value) { assert( - value != null && (value.isInline || value.isIgnored || value.isEmpty), + (value.isInline || value.isIgnored || value.isEmpty), 'Unable to apply Style to leaf: $value'); super.applyStyle(value); } @@ -52,7 +52,7 @@ abstract class Leaf extends Node { @override insert(int index, Object data, Style? style) { - assert(data != null && index >= 0 && index <= length); + assert(index >= 0 && index <= length); Leaf node = Leaf(data); if (index < length) { splitAt(index)!.insertBefore(node); diff --git a/lib/models/documents/nodes/node.dart b/lib/models/documents/nodes/node.dart index ccd5a1c2..984ed0a1 100644 --- a/lib/models/documents/nodes/node.dart +++ b/lib/models/documents/nodes/node.dart @@ -19,9 +19,6 @@ abstract class Node extends LinkedListEntry { } void applyStyle(Style value) { - if (value == null) { - throw ArgumentError('null value'); - } _style = _style.mergeAll(value); } diff --git a/lib/models/quill_delta.dart b/lib/models/quill_delta.dart index 643605cc..4e61ed3d 100644 --- a/lib/models/quill_delta.dart +++ b/lib/models/quill_delta.dart @@ -51,7 +51,7 @@ class Operation { final Map? _attributes; Operation._(this.key, this.length, this.data, Map? attributes) - : assert(key != null && length != null && data != null), + : assert(_validKeys.contains(key), 'Invalid operation key "$key".'), assert(() { if (key != Operation.insertKey) return true; @@ -243,7 +243,7 @@ class Delta { int _modificationCount = 0; Delta._(List operations) - : assert(operations != null), + : _operations = operations; /// Creates new empty [Delta]. @@ -312,7 +312,6 @@ class Delta { /// Insert [data] at current position. void insert(dynamic data, [Map? attributes]) { - assert(data != null); if (data is String && data.isEmpty) return; // no-op push(Operation.insert(data, attributes)); } @@ -326,7 +325,7 @@ class Delta { void _mergeWithTail(Operation operation) { assert(isNotEmpty); - assert(operation != null && last.key == operation.key); + assert(last.key == operation.key); assert(operation.data is String && last.data is String); final length = operation.length! + last.length!; @@ -640,8 +639,6 @@ class DeltaIterator { /// Optional [length] specifies maximum length of operation to return. Note /// that actual length of returned operation may be less than specified value. Operation next([int length = 4294967296]) { - assert(length != null); - if (_modificationCount != delta._modificationCount) { throw ConcurrentModificationError(delta); } diff --git a/lib/models/rules/insert.dart b/lib/models/rules/insert.dart index 41575c41..ea4f4925 100644 --- a/lib/models/rules/insert.dart +++ b/lib/models/rules/insert.dart @@ -341,9 +341,7 @@ class PreserveInlineStylesRule extends InsertRule { ..retain(index) ..insert(text, attributes.isEmpty ? null : attributes); Operation next = itr.next(); - if (next == null) { - return delta; - } + Map nextAttributes = next.attributes ?? const {}; if (!nextAttributes.containsKey(Attribute.link.key)) { diff --git a/lib/models/rules/rule.dart b/lib/models/rules/rule.dart index f42064b7..a549c22f 100644 --- a/lib/models/rules/rule.dart +++ b/lib/models/rules/rule.dart @@ -13,8 +13,6 @@ abstract class Rule { Delta? apply(Delta document, int index, {int? len, Object? data, Attribute? attribute}) { - assert(document != null); - assert(index != null); validateArgs(len, data, attribute); return applyRule(document, index, len: len, data: data, attribute: attribute); diff --git a/lib/widgets/controller.dart b/lib/widgets/controller.dart index 719b26be..fbb9c7ff 100644 --- a/lib/widgets/controller.dart +++ b/lib/widgets/controller.dart @@ -15,8 +15,7 @@ class QuillController extends ChangeNotifier { Style toggledStyle = Style(); QuillController({required this.document, required this.selection}) - : assert(document != null), - assert(selection != null); + ; factory QuillController.basic() { return QuillController( @@ -86,7 +85,7 @@ class QuillController extends ChangeNotifier { print('document.replace failed: $e'); throw e; } - bool shouldRetainDelta = delta != null && + bool shouldRetainDelta = toggledStyle.isNotEmpty && delta.isNotEmpty && delta.length <= 2 && @@ -165,9 +164,7 @@ class QuillController extends ChangeNotifier { if (delta.isNotEmpty) { document.compose(delta, source); } - if (textSelection != null) { - _updateSelection(textSelection, source); - } else { + textSelection = selection.copyWith( baseOffset: delta.transformPosition(selection.baseOffset, force: false), @@ -176,7 +173,7 @@ class QuillController extends ChangeNotifier { if (selection != textSelection) { _updateSelection(textSelection, source); } - } + notifyListeners(); } @@ -187,8 +184,6 @@ class QuillController extends ChangeNotifier { } _updateSelection(TextSelection textSelection, ChangeSource source) { - assert(textSelection != null); - assert(source != null); selection = textSelection; int end = document.length - 1; selection = selection.copyWith( diff --git a/lib/widgets/default_styles.dart b/lib/widgets/default_styles.dart index b773d68c..6402743b 100644 --- a/lib/widgets/default_styles.dart +++ b/lib/widgets/default_styles.dart @@ -9,8 +9,7 @@ class QuillStyles extends InheritedWidget { Key? key, required this.data, required Widget child, - }) : assert(data != null), - assert(child != null), + }) : super(key: key, child: child); @override diff --git a/lib/widgets/delegate.dart b/lib/widgets/delegate.dart index a4258518..0898c4f5 100644 --- a/lib/widgets/delegate.dart +++ b/lib/widgets/delegate.dart @@ -22,7 +22,7 @@ class EditorTextSelectionGestureDetectorBuilder { bool shouldShowSelectionToolbar = true; EditorTextSelectionGestureDetectorBuilder(this.delegate) - : assert(delegate != null); + ; EditorState? getEditor() { return delegate.getEditableTextKey().currentState; diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index 5b0260a0..a5faebf6 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -182,13 +182,7 @@ class QuillEditor extends StatefulWidget { this.onLaunchUrl, this.embedBuilder = kIsWeb ? _defaultEmbedBuilderWeb : _defaultEmbedBuilder}) - : assert(controller != null), - assert(scrollController != null), - assert(scrollable != null), - assert(focusNode != null), - assert(autoFocus != null), - assert(readOnly != null), - assert(embedBuilder != null); + ; factory QuillEditor.basic( {required QuillController controller, required bool readOnly}) { @@ -559,10 +553,7 @@ class RenderEditor extends RenderEditableContainerBox this._startHandleLayerLink, this._endHandleLayerLink, EdgeInsets floatingCursorAddedMargin) - : assert(document != null), - assert(textDirection != null), - assert(_hasFocus != null), - assert(floatingCursorAddedMargin != null), + : super( children, document.root, @@ -571,7 +562,6 @@ class RenderEditor extends RenderEditableContainerBox ); setDocument(Document doc) { - assert(doc != null); if (document == doc) { return; } @@ -580,7 +570,6 @@ class RenderEditor extends RenderEditableContainerBox } setHasFocus(bool h) { - assert(h != null); if (_hasFocus == h) { return; } @@ -615,7 +604,6 @@ class RenderEditor extends RenderEditableContainerBox @override List getEndpointsForSelection( TextSelection textSelection) { - assert(constraints != null); if (textSelection.isCollapsed) { RenderEditableBox child = childAtPosition(textSelection.extent); @@ -686,11 +674,6 @@ class RenderEditor extends RenderEditableContainerBox Offset? to, SelectionChangedCause cause, ) { - assert(cause != null); - assert(from != null); - if (onSelectionChanged == null) { - return; - } TextPosition firstPosition = getPositionForOffset(from); TextSelection firstWord = selectWordAtPosition(firstPosition); TextSelection lastWord = @@ -718,18 +701,12 @@ class RenderEditor extends RenderEditableContainerBox !focusingEmpty) { return; } - if (onSelectionChanged != null) { onSelectionChanged(nextSelection, cause); - } } @override selectWordEdge(SelectionChangedCause cause) { - assert(cause != null); assert(_lastTapDownPosition != null); - if (onSelectionChanged == null) { - return; - } TextPosition position = getPositionForOffset(_lastTapDownPosition!); RenderEditableBox child = childAtPosition(position); int nodeOffset = child.getContainer().getOffset(); @@ -763,11 +740,6 @@ class RenderEditor extends RenderEditableContainerBox Offset? to, SelectionChangedCause cause, ) { - assert(cause != null); - assert(from != null); - if (onSelectionChanged == null) { - return; - } TextPosition fromPosition = getPositionForOffset(from); TextPosition? toPosition = to == null ? null : getPositionForOffset(to); @@ -932,9 +904,7 @@ class RenderEditableContainerBox extends RenderBox RenderEditableContainerBox(List? children, this._container, this.textDirection, this._padding) - : assert(_container != null), - assert(textDirection != null), - assert(_padding != null), + : assert(_padding.isNonNegative) { addAll(children); } @@ -944,7 +914,6 @@ class RenderEditableContainerBox extends RenderBox } setContainer(containerNode.Container c) { - assert(c != null); if (_container == c) { return; } @@ -955,7 +924,6 @@ class RenderEditableContainerBox extends RenderBox EdgeInsetsGeometry getPadding() => _padding; setPadding(EdgeInsetsGeometry value) { - assert(value != null); assert(value.isNonNegative); if (_padding == value) { return; diff --git a/lib/widgets/keyboard_listener.dart b/lib/widgets/keyboard_listener.dart index c408c446..81c9b5db 100644 --- a/lib/widgets/keyboard_listener.dart +++ b/lib/widgets/keyboard_listener.dart @@ -59,9 +59,7 @@ class KeyboardListener { }; KeyboardListener(this.onCursorMove, this.onShortcut, this.onDelete) - : assert(onCursorMove != null), - assert(onShortcut != null), - assert(onDelete != null); + ; bool handleRawKeyEvent(RawKeyEvent event) { if (event is! RawKeyDownEvent) { diff --git a/lib/widgets/raw_editor.dart b/lib/widgets/raw_editor.dart index 08a6f5d3..5829e2ed 100644 --- a/lib/widgets/raw_editor.dart +++ b/lib/widgets/raw_editor.dart @@ -907,7 +907,6 @@ class RawEditorState extends EditorState _selectionOverlay?.hide(); _selectionOverlay = null; - if (widget.selectionCtrls != null) { _selectionOverlay = EditorTextSelectionOverlay( textEditingValue, false, @@ -924,7 +923,6 @@ class RawEditorState extends EditorState _clipboardStatus); _selectionOverlay!.handlesVisible = _shouldShowSelectionHandles(); _selectionOverlay!.showHandles(); - } } } diff --git a/lib/widgets/text_block.dart b/lib/widgets/text_block.dart index 6c610fdc..ac6b4853 100644 --- a/lib/widgets/text_block.dart +++ b/lib/widgets/text_block.dart @@ -74,9 +74,7 @@ class EditableTextBlock extends StatelessWidget { this.embedBuilder, this.cursorCont, this.indentLevelCounts) - : assert(hasFocus != null), - assert(embedBuilder != null), - assert(cursorCont != null); + ; @override Widget build(BuildContext context) { @@ -263,11 +261,7 @@ class RenderEditableTextBlock extends RenderEditableContainerBox required Decoration decoration, ImageConfiguration configuration = ImageConfiguration.empty, EdgeInsets contentPadding = EdgeInsets.zero, - }) : assert(block != null), - assert(textDirection != null), - assert(decoration != null), - assert(padding != null), - assert(contentPadding != null), + }) : _decoration = decoration, _configuration = configuration, _savedPadding = padding, @@ -283,7 +277,6 @@ class RenderEditableTextBlock extends RenderEditableContainerBox EdgeInsets _contentPadding; set contentPadding(EdgeInsets value) { - assert(value != null); if (_contentPadding == value) return; _contentPadding = value; super.setPadding(_savedPadding.add(_contentPadding)); @@ -301,7 +294,6 @@ class RenderEditableTextBlock extends RenderEditableContainerBox Decoration _decoration; set decoration(Decoration value) { - assert(value != null); if (value == _decoration) return; _painter?.dispose(); _painter = null; @@ -313,7 +305,6 @@ class RenderEditableTextBlock extends RenderEditableContainerBox ImageConfiguration _configuration; set configuration(ImageConfiguration value) { - assert(value != null); if (value == _configuration) return; _configuration = value; markNeedsPaint(); @@ -495,8 +486,6 @@ class RenderEditableTextBlock extends RenderEditableContainerBox } _paintDecoration(PaintingContext context, Offset offset) { - assert(size.width != null); - assert(size.height != null); _painter ??= _decoration.createBoxPainter(markNeedsPaint); EdgeInsets decorationPadding = resolvedPadding! - _contentPadding; @@ -531,11 +520,7 @@ class _EditableBlock extends MultiChildRenderObjectWidget { _EditableBlock(this.block, this.textDirection, this.padding, this.decoration, this.contentPadding, List children) - : assert(block != null), - assert(textDirection != null), - assert(padding != null), - assert(decoration != null), - assert(children != null), + : super(children: children); EdgeInsets get _padding => diff --git a/lib/widgets/text_selection.dart b/lib/widgets/text_selection.dart index ebb873d3..5fccd888 100644 --- a/lib/widgets/text_selection.dart +++ b/lib/widgets/text_selection.dart @@ -55,13 +55,9 @@ class EditorTextSelectionOverlay { this.dragStartBehavior, this.onSelectionHandleTapped, this.clipboardStatus) - : assert(value != null), - assert(context != null), - assert(handlesVisible != null) { + { OverlayState overlay = Overlay.of(context, rootOverlay: true)!; - assert( - overlay != null, - ); + _toolbarController = AnimationController( duration: Duration(milliseconds: 150), vsync: overlay); } @@ -71,7 +67,6 @@ class EditorTextSelectionOverlay { Animation get _toolbarOpacity => _toolbarController.view; setHandlesVisible(bool visible) { - assert(visible != null); if (handlesVisible == visible) { return; } @@ -111,8 +106,8 @@ class EditorTextSelectionOverlay { Widget _buildHandle( BuildContext context, _TextSelectionHandlePosition position) { if ((_selection.isCollapsed && - position == _TextSelectionHandlePosition.END) || - selectionCtrls == null) { + position == _TextSelectionHandlePosition.END) + ) { return Container(); } return Visibility( @@ -167,9 +162,7 @@ class EditorTextSelectionOverlay { } Widget _buildToolbar(BuildContext context) { - if (selectionCtrls == null) { - return Container(); - } + List endpoints = renderObject!.getEndpointsForSelection(_selection); @@ -289,7 +282,6 @@ class _TextSelectionHandleOverlay extends StatefulWidget { case _TextSelectionHandlePosition.END: return renderObject!.selectionEndInViewport; } - return null; } } @@ -468,14 +460,12 @@ class _TextSelectionHandleOverlayState ) { if (widget.selection.isCollapsed) return TextSelectionHandleType.collapsed; - assert(textDirection != null); switch (textDirection) { case TextDirection.ltr: return ltrType; case TextDirection.rtl: return rtlType; } - return null; } } @@ -496,7 +486,7 @@ class EditorTextSelectionGestureDetector extends StatefulWidget { this.onDragSelectionEnd, this.behavior, required this.child, - }) : assert(child != null), + }) : super(key: key); final GestureTapDownCallback? onTapDown; @@ -660,7 +650,6 @@ class _EditorTextSelectionGestureDetectorState } bool _isWithinDoubleTapTolerance(Offset secondTapOffset) { - assert(secondTapOffset != null); if (_lastTapOffset == null) { return false; } diff --git a/lib/widgets/toolbar.dart b/lib/widgets/toolbar.dart index c4fad890..b8d7ed23 100644 --- a/lib/widgets/toolbar.dart +++ b/lib/widgets/toolbar.dart @@ -186,11 +186,7 @@ class ToggleStyleButton extends StatefulWidget { required this.icon, required this.controller, this.childBuilder = defaultToggleStyleButtonBuilder, - }) : assert(attribute.value != null), - assert(icon != null), - assert(controller != null), - assert(childBuilder != null), - super(key: key); + }) : super(key: key); @override _ToggleStyleButtonState createState() => _ToggleStyleButtonState(); @@ -274,9 +270,7 @@ class ToggleCheckListButton extends StatefulWidget { required this.controller, this.childBuilder = defaultToggleStyleButtonBuilder, required this.attribute, - }) : assert(icon != null), - assert(controller != null), - assert(childBuilder != null), + }) : super(key: key); @override @@ -502,8 +496,7 @@ class ImageButton extends StatefulWidget { required this.controller, required this.imageSource, this.onImagePickCallback}) - : assert(icon != null), - assert(controller != null), + : super(key: key); @override @@ -522,7 +515,6 @@ class _ImageButtonState extends State { final File file = File(pickedFile.path); - if (file == null || widget.onImagePickCallback == null) return null; // We simply return the absolute path to selected file. try { String url = await widget.onImagePickCallback!(file); @@ -554,7 +546,6 @@ class _ImageButtonState extends State { if (_paths != null) { File file = File(_fileName); - if (file == null || widget.onImagePickCallback == null) return null; // We simply return the absolute path to selected file. try { String url = await widget.onImagePickCallback!(file); @@ -611,9 +602,7 @@ class ColorButton extends StatefulWidget { required this.icon, required this.controller, required this.background}) - : assert(icon != null), - assert(controller != null), - assert(background != null), + : super(key: key); @override @@ -752,9 +741,7 @@ class HistoryButton extends StatefulWidget { required this.icon, required this.controller, required this.undo}) - : assert(icon != null), - assert(controller != null), - assert(undo != null), + : super(key: key); @override @@ -827,9 +814,7 @@ class IndentButton extends StatefulWidget { required this.icon, required this.controller, required this.isIncrease}) - : assert(icon != null), - assert(controller != null), - assert(isIncrease != null), + : super(key: key); @override @@ -881,8 +866,7 @@ class ClearFormatButton extends StatefulWidget { final QuillController controller; ClearFormatButton({Key? key, required this.icon, required this.controller}) - : assert(icon != null), - assert(controller != null), + : super(key: key); @override @@ -1265,9 +1249,6 @@ class _QuillDropdownButtonState extends State> { // if (widget.onCanceled != null) widget.onCanceled(); return null; } - if (widget.onSelected != null) { - widget.onSelected(newValue); - } }); }