From 0d8d4f77aa2d174a4b73bfa8238153a2af024562 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Thu, 8 Oct 2020 09:26:30 +0200 Subject: [PATCH] unnecessary null comparison (#67525) --- .../lib/src/material/refresh_indicator.dart | 5 +- .../flutter/lib/src/material/scaffold.dart | 52 ++++++++----------- .../lib/src/services/binary_messenger.dart | 4 +- .../flutter/lib/src/services/binding.dart | 2 +- .../lib/src/services/platform_messages.dart | 2 +- .../flutter/test/rendering/mock_canvas.dart | 12 +---- .../flutter_driver/lib/src/common/find.dart | 3 +- .../lib/src/extension/wait_conditions.dart | 8 +-- packages/flutter_test/lib/src/binding.dart | 11 ++-- .../flutter_test/lib/src/widget_tester.dart | 3 +- 10 files changed, 38 insertions(+), 64 deletions(-) diff --git a/packages/flutter/lib/src/material/refresh_indicator.dart b/packages/flutter/lib/src/material/refresh_indicator.dart index 86390ca4c12..55b8b9a14fa 100644 --- a/packages/flutter/lib/src/material/refresh_indicator.dart +++ b/packages/flutter/lib/src/material/refresh_indicator.dart @@ -378,7 +378,10 @@ class RefreshIndicatorState extends State with TickerProviderS )); return true; }()); - if (refreshResult == null) + // `refreshResult` has a non-nullable type, but might be null when + // running with weak checking, so we need to null check it anyway (and + // ignore the warning that the null-handling logic is dead code). + if (refreshResult == null) // ignore: dead_code return; refreshResult.whenComplete(() { if (mounted && _mode == _RefreshIndicatorMode.refresh) { diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 767bb125d87..12fce195b9c 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -3326,38 +3326,28 @@ class _StandardBottomSheetState extends State<_StandardBottomSheet> { @override Widget build(BuildContext context) { - if (widget.animationController != null) { - return AnimatedBuilder( - animation: widget.animationController, - builder: (BuildContext context, Widget? child) { - return Align( - alignment: AlignmentDirectional.topStart, - heightFactor: animationCurve.transform(widget.animationController.value), - child: child, - ); - }, - child: _wrapBottomSheet( - BottomSheet( - animationController: widget.animationController, - enableDrag: widget.enableDrag, - onDragStart: _handleDragStart, - onDragEnd: _handleDragEnd, - onClosing: widget.onClosing!, - builder: widget.builder, - backgroundColor: widget.backgroundColor, - elevation: widget.elevation, - shape: widget.shape, - clipBehavior: widget.clipBehavior, - ), + return AnimatedBuilder( + animation: widget.animationController, + builder: (BuildContext context, Widget? child) { + return Align( + alignment: AlignmentDirectional.topStart, + heightFactor: animationCurve.transform(widget.animationController.value), + child: child, + ); + }, + child: _wrapBottomSheet( + BottomSheet( + animationController: widget.animationController, + enableDrag: widget.enableDrag, + onDragStart: _handleDragStart, + onDragEnd: _handleDragEnd, + onClosing: widget.onClosing!, + builder: widget.builder, + backgroundColor: widget.backgroundColor, + elevation: widget.elevation, + shape: widget.shape, + clipBehavior: widget.clipBehavior, ), - ); - } - - return _wrapBottomSheet( - BottomSheet( - onClosing: widget.onClosing!, - builder: widget.builder, - backgroundColor: widget.backgroundColor, ), ); } diff --git a/packages/flutter/lib/src/services/binary_messenger.dart b/packages/flutter/lib/src/services/binary_messenger.dart index 76ca6b2bfb0..7b242754723 100644 --- a/packages/flutter/lib/src/services/binary_messenger.dart +++ b/packages/flutter/lib/src/services/binary_messenger.dart @@ -10,7 +10,7 @@ import 'package:flutter/foundation.dart'; import 'binding.dart'; /// A function which takes a platform message and asynchronously returns an encoded response. -typedef MessageHandler = Future Function(ByteData? message); +typedef MessageHandler = Future? Function(ByteData? message); /// A messenger which sends binary data across the Flutter platform barrier. /// @@ -31,7 +31,7 @@ abstract class BinaryMessenger { /// /// Returns a [Future] which completes to the received response, undecoded, /// in binary form. - Future send(String channel, ByteData? message); + Future? send(String channel, ByteData? message); /// Set a callback for receiving messages from the platform plugins on the /// given channel, without decoding them. diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index 476b6a7147d..961a99c168c 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -300,7 +300,7 @@ class _DefaultBinaryMessenger extends BinaryMessenger { } @override - Future send(String channel, ByteData? message) { + Future? send(String channel, ByteData? message) { final MessageHandler? handler = _mockHandlers[channel]; if (handler != null) return handler(message); diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart index 9278796cb74..96e2a9b0a60 100644 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ b/packages/flutter/lib/src/services/platform_messages.dart @@ -65,7 +65,7 @@ class BinaryMessages { 'Use defaultBinaryMessenger.send instead. ' 'This feature was deprecated after v1.6.5.' ) - static Future send(String channel, ByteData? message) { + static Future? send(String channel, ByteData? message) { return _binaryMessenger.send(channel, message); } diff --git a/packages/flutter/test/rendering/mock_canvas.dart b/packages/flutter/test/rendering/mock_canvas.dart index 824319301bd..dceae4b7cfc 100644 --- a/packages/flutter/test/rendering/mock_canvas.dart +++ b/packages/flutter/test/rendering/mock_canvas.dart @@ -821,10 +821,6 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher imp final Iterator call = calls.iterator..moveNext(); try { while (predicate.moveNext()) { - if (call.current == null) { - throw 'It painted less on its canvas than the paint pattern expected. ' - 'The first missing paint call was: ${predicate.current}'; - } predicate.current.match(call); } assert(predicate.current == null); @@ -836,11 +832,7 @@ class _TestRecordingCanvasPatternMatcher extends _TestRecordingCanvasMatcher imp return false; } on String catch (s) { description.writeln(s); - if (call.current != null) { - description.write('The stack of the offending call was:\n${call.current.stackToString(indent: " ")}\n'); - } else { - description.write('The stack of the first call was:\n${calls.first.stackToString(indent: " ")}\n'); - } + description.write('The stack of the offending call was:\n${call.current.stackToString(indent: " ")}\n'); return false; } return true; @@ -1390,8 +1382,6 @@ class _SomethingPaintPredicate extends _PaintPredicate { RecordedInvocation currentCall; do { currentCall = call.current; - if (currentCall == null) - throw 'It did not call anything that was matched by the predicate passed to a "something" step of the paint pattern.'; if (!currentCall.invocation.isMethod) throw 'It called $currentCall, which was not a method, when the paint pattern expected a method call'; call.moveNext(); diff --git a/packages/flutter_driver/lib/src/common/find.dart b/packages/flutter_driver/lib/src/common/find.dart index cdeeadef6d4..3de48625eef 100644 --- a/packages/flutter_driver/lib/src/common/find.dart +++ b/packages/flutter_driver/lib/src/common/find.dart @@ -42,8 +42,7 @@ DriverError _createInvalidKeyValueTypeError(String invalidType) { abstract class CommandWithTarget extends Command { /// Constructs this command given a [finder]. CommandWithTarget(this.finder, {Duration timeout}) : super(timeout: timeout) { - if (finder == null) - throw DriverError('$runtimeType target cannot be null'); + assert(finder != null, '$runtimeType target cannot be null'); } /// Deserializes this command from the value generated by [serialize]. diff --git a/packages/flutter_driver/lib/src/extension/wait_conditions.dart b/packages/flutter_driver/lib/src/extension/wait_conditions.dart index fa7dac00329..8b1e1314a48 100644 --- a/packages/flutter_driver/lib/src/extension/wait_conditions.dart +++ b/packages/flutter_driver/lib/src/extension/wait_conditions.dart @@ -164,13 +164,7 @@ class _InternalCombinedCondition implements WaitCondition { if (condition.conditionName != 'CombinedCondition') throw SerializationException('Error occurred during deserializing from the given condition: ${condition.serialize()}'); final CombinedCondition combinedCondition = condition as CombinedCondition; - if (combinedCondition.conditions == null) { - return const _InternalCombinedCondition([]); - } - - final List conditions = combinedCondition.conditions.map( - (SerializableWaitCondition serializableCondition) => deserializeCondition(serializableCondition) - ).toList(); + final List conditions = combinedCondition.conditions.map(deserializeCondition).toList(); return _InternalCombinedCondition(conditions); } diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index c93f533b6a2..0b76a0b9888 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -101,11 +101,11 @@ class TestDefaultBinaryMessenger extends BinaryMessenger { int get pendingMessageCount => _pendingMessages.length; @override - Future send(String channel, ByteData? message) { - final Future resultFuture = delegate.send(channel, message); - // Removes the future itself from the [_pendingMessages] list when it - // completes. + Future? send(String channel, ByteData? message) { + final Future? resultFuture = delegate.send(channel, message); if (resultFuture != null) { + // Removes the future itself from the [_pendingMessages] list when it + // completes. _pendingMessages.add(resultFuture); resultFuture.whenComplete(() => _pendingMessages.remove(resultFuture)); } @@ -1780,8 +1780,7 @@ class _LiveTestRenderView extends RenderView { _label ??= TextPainter(textAlign: TextAlign.left, textDirection: TextDirection.ltr); _label!.text = TextSpan(text: value, style: _labelStyle); _label!.layout(); - if (onNeedPaint != null) - onNeedPaint(); + onNeedPaint(); } @override diff --git a/packages/flutter_test/lib/src/widget_tester.dart b/packages/flutter_test/lib/src/widget_tester.dart index 1efed5c1676..e4d42c536c5 100644 --- a/packages/flutter_test/lib/src/widget_tester.dart +++ b/packages/flutter_test/lib/src/widget_tester.dart @@ -1072,8 +1072,7 @@ class _TestTicker extends Ticker { @override void dispose() { - if (_onDispose != null) - _onDispose(this); + _onDispose(this); super.dispose(); } }