From 4fa32df141d6c41e75a8ed03a4e864b39069bf39 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Thu, 16 May 2019 22:25:51 +0200 Subject: [PATCH] use null aware operators (#32711) * use null aware operators * rollback changes about null-aware operator * disable lint prefer_is_not_empty --- analysis_options.yaml | 2 +- dev/devicelab/lib/framework/adb.dart | 5 +---- .../flutter/lib/src/material/data_table.dart | 2 +- packages/flutter/lib/src/material/list_tile.dart | 2 +- .../flutter/lib/src/painting/image_stream.dart | 2 +- packages/flutter/lib/src/rendering/box.dart | 16 ++++++++-------- .../flutter/lib/src/rendering/proxy_box.dart | 4 ++-- .../flutter/lib/src/semantics/semantics.dart | 12 ++++++------ .../flutter/lib/src/widgets/fade_in_image.dart | 2 +- packages/flutter/lib/src/widgets/image.dart | 2 +- .../test/widgets/slivers_keepalive_test.dart | 4 +--- packages/flutter_tools/lib/src/base/config.dart | 3 +-- .../flutter_tools/lib/src/ios/simulators.dart | 2 +- .../lib/src/test/event_printer.dart | 2 +- packages/flutter_tools/lib/src/version.dart | 2 +- packages/flutter_tools/lib/src/vmservice.dart | 6 ++---- packages/flutter_tools/test/src/context.dart | 2 +- 17 files changed, 31 insertions(+), 39 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 2af2f9612d..5b6de5da1d 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -139,7 +139,7 @@ linter: # - prefer_int_literals # not yet tested # - prefer_interpolation_to_compose_strings # not yet tested - prefer_is_empty - - prefer_is_not_empty + # - prefer_is_not_empty # disable until NNBD, see https://github.com/flutter/flutter/pull/32711#issuecomment-492930932 - prefer_iterable_whereType # - prefer_mixin # https://github.com/dart-lang/language/issues/32 - prefer_null_aware_operators diff --git a/dev/devicelab/lib/framework/adb.dart b/dev/devicelab/lib/framework/adb.dart index 3f0bbded58..9e4b8f0748 100644 --- a/dev/devicelab/lib/framework/adb.dart +++ b/dev/devicelab/lib/framework/adb.dart @@ -470,10 +470,7 @@ class IosDevice implements Device { /// Path to the `adb` executable. String get adbPath { - final String androidHome = - Platform.environment['ANDROID_HOME'] != null - ? Platform.environment['ANDROID_HOME'] - : Platform.environment['ANDROID_SDK_ROOT']; + final String androidHome = Platform.environment['ANDROID_HOME'] ?? Platform.environment['ANDROID_SDK_ROOT']; if (androidHome == null) throw 'The ANDROID_SDK_ROOT and ANDROID_HOME environment variables are ' diff --git a/packages/flutter/lib/src/material/data_table.dart b/packages/flutter/lib/src/material/data_table.dart index 64a2f2fb57..7b02d2371a 100644 --- a/packages/flutter/lib/src/material/data_table.dart +++ b/packages/flutter/lib/src/material/data_table.dart @@ -739,7 +739,7 @@ class _SortArrowState extends State<_SortArrow> with TickerProviderStateMixin { void didUpdateWidget(_SortArrow oldWidget) { super.didUpdateWidget(oldWidget); bool skipArrow = false; - final bool newDown = widget.down != null ? widget.down : _down; + final bool newDown = widget.down ?? _down; if (oldWidget.visible != widget.visible) { if (widget.visible && (_opacityController.status == AnimationStatus.dismissed)) { _orientationController.stop(); diff --git a/packages/flutter/lib/src/material/list_tile.dart b/packages/flutter/lib/src/material/list_tile.dart index f0bf07e06d..fb7fc4b004 100644 --- a/packages/flutter/lib/src/material/list_tile.dart +++ b/packages/flutter/lib/src/material/list_tile.dart @@ -782,7 +782,7 @@ class ListTile extends StatelessWidget { } bool _isDenseLayout(ListTileTheme tileTheme) { - return dense != null ? dense : (tileTheme?.dense ?? false); + return dense ?? tileTheme?.dense ?? false; } TextStyle _titleTextStyle(ThemeData theme, ListTileTheme tileTheme) { diff --git a/packages/flutter/lib/src/painting/image_stream.dart b/packages/flutter/lib/src/painting/image_stream.dart index eeb2d0ea72..af16e10d36 100644 --- a/packages/flutter/lib/src/painting/image_stream.dart +++ b/packages/flutter/lib/src/painting/image_stream.dart @@ -202,7 +202,7 @@ class ImageStream extends Diagnosticable { /// will go from being different than other [ImageStream]'s keys to /// potentially being the same as others'. No notification is sent when this /// happens. - Object get key => _completer != null ? _completer : this; + Object get key => _completer ?? this; @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 16817da8e1..abcc350d29 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -109,10 +109,10 @@ class BoxConstraints extends Constraints { const BoxConstraints.tightFor({ double width, double height, - }) : minWidth = width != null ? width : 0.0, - maxWidth = width != null ? width : double.infinity, - minHeight = height != null ? height : 0.0, - maxHeight = height != null ? height : double.infinity; + }) : minWidth = width ?? 0.0, + maxWidth = width ?? double.infinity, + minHeight = height ?? 0.0, + maxHeight = height ?? double.infinity; /// Creates box constraints that require the given width or height, except if /// they are infinite. @@ -143,10 +143,10 @@ class BoxConstraints extends Constraints { const BoxConstraints.expand({ double width, double height, - }) : minWidth = width != null ? width : double.infinity, - maxWidth = width != null ? width : double.infinity, - minHeight = height != null ? height : double.infinity, - maxHeight = height != null ? height : double.infinity; + }) : minWidth = width ?? double.infinity, + maxWidth = width ?? double.infinity, + minHeight = height ?? double.infinity, + maxHeight = height ?? double.infinity; /// The minimum width that satisfies the constraints. final double minWidth; diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 3a059c2b9a..7a6bf1ed51 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -2965,7 +2965,7 @@ class RenderIgnorePointer extends RenderProxyBox { markNeedsSemanticsUpdate(); } - bool get _effectiveIgnoringSemantics => ignoringSemantics == null ? ignoring : ignoringSemantics; + bool get _effectiveIgnoringSemantics => ignoringSemantics ?? ignoring; @override bool hitTest(BoxHitTestResult result, { Offset position }) { @@ -3171,7 +3171,7 @@ class RenderAbsorbPointer extends RenderProxyBox { markNeedsSemanticsUpdate(); } - bool get _effectiveIgnoringSemantics => ignoringSemantics == null ? absorbing : ignoringSemantics; + bool get _effectiveIgnoringSemantics => ignoringSemantics ?? absorbing; @override bool hitTest(BoxHitTestResult result, { Offset position }) { diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index 19bc64c643..79ddc24f8b 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -1949,12 +1949,12 @@ class SemanticsNode extends AbstractNode with DiagnosticableTreeMixin { textDirection: data.textDirection, textSelectionBase: data.textSelection != null ? data.textSelection.baseOffset : -1, textSelectionExtent: data.textSelection != null ? data.textSelection.extentOffset : -1, - platformViewId: data.platformViewId != null ? data.platformViewId : -1, - scrollChildren: data.scrollChildCount != null ? data.scrollChildCount : 0, - scrollIndex: data.scrollIndex != null ? data.scrollIndex : 0 , - scrollPosition: data.scrollPosition != null ? data.scrollPosition : double.nan, - scrollExtentMax: data.scrollExtentMax != null ? data.scrollExtentMax : double.nan, - scrollExtentMin: data.scrollExtentMin != null ? data.scrollExtentMin : double.nan, + platformViewId: data.platformViewId ?? -1, + scrollChildren: data.scrollChildCount ?? 0, + scrollIndex: data.scrollIndex ?? 0 , + scrollPosition: data.scrollPosition ?? double.nan, + scrollExtentMax: data.scrollExtentMax ?? double.nan, + scrollExtentMin: data.scrollExtentMin ?? double.nan, transform: data.transform?.storage ?? _kIdentityTransform, elevation: data.elevation, thickness: data.thickness, diff --git a/packages/flutter/lib/src/widgets/fade_in_image.dart b/packages/flutter/lib/src/widgets/fade_in_image.dart index 75e260fa06..dded9e4424 100644 --- a/packages/flutter/lib/src/widgets/fade_in_image.dart +++ b/packages/flutter/lib/src/widgets/fade_in_image.dart @@ -558,7 +558,7 @@ class _FadeInImageState extends State with TickerProviderStateMixin return Semantics( container: _semanticLabel != null, image: true, - label: _semanticLabel == null ? '' : _semanticLabel, + label: _semanticLabel ?? '', child: image, ); } diff --git a/packages/flutter/lib/src/widgets/image.dart b/packages/flutter/lib/src/widgets/image.dart index 4bfbb6bd5d..c272aff3ce 100644 --- a/packages/flutter/lib/src/widgets/image.dart +++ b/packages/flutter/lib/src/widgets/image.dart @@ -710,7 +710,7 @@ class _ImageState extends State { return Semantics( container: widget.semanticLabel != null, image: true, - label: widget.semanticLabel == null ? '' : widget.semanticLabel, + label: widget.semanticLabel ?? '', child: image, ); } diff --git a/packages/flutter/test/widgets/slivers_keepalive_test.dart b/packages/flutter/test/widgets/slivers_keepalive_test.dart index 53750baf3e..83a05a67a7 100644 --- a/packages/flutter/test/widgets/slivers_keepalive_test.dart +++ b/packages/flutter/test/widgets/slivers_keepalive_test.dart @@ -460,9 +460,7 @@ class _SwitchingChildBuilderTest extends State { return children[index]; }, childCount: children.length, - findChildIndexCallback: (Key key) { - return _mapKeyToIndex[key] == null ? -1 : _mapKeyToIndex[key]; - } + findChildIndexCallback: (Key key) => _mapKeyToIndex[key] ?? -1, ), ) ], diff --git a/packages/flutter_tools/lib/src/base/config.dart b/packages/flutter_tools/lib/src/base/config.dart index 388e989f79..b3a505d9e5 100644 --- a/packages/flutter_tools/lib/src/base/config.dart +++ b/packages/flutter_tools/lib/src/base/config.dart @@ -46,6 +46,5 @@ class Config { String _userHomeDir() { final String envKey = platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME'; - final String value = platform.environment[envKey]; - return value == null ? '.' : value; + return platform.environment[envKey] ?? '.'; } diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 428e154b67..5fed6aca61 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -288,7 +288,7 @@ class IOSSimulator extends Device { if (isSupported()) return 'Supported'; - return _supportMessage != null ? _supportMessage : 'Unknown'; + return _supportMessage ?? 'Unknown'; } @override diff --git a/packages/flutter_tools/lib/src/test/event_printer.dart b/packages/flutter_tools/lib/src/test/event_printer.dart index dee2d00295..a3f4cc1b91 100644 --- a/packages/flutter_tools/lib/src/test/event_printer.dart +++ b/packages/flutter_tools/lib/src/test/event_printer.dart @@ -8,7 +8,7 @@ import 'watcher.dart'; /// Prints JSON events when running a test in --machine mode. class EventPrinter extends TestWatcher { - EventPrinter({StringSink out}) : _out = out == null ? stdout: out; + EventPrinter({StringSink out}) : _out = out ?? stdout; final StringSink _out; diff --git a/packages/flutter_tools/lib/src/version.dart b/packages/flutter_tools/lib/src/version.dart index b2ef9c4a7b..6d1503f69d 100644 --- a/packages/flutter_tools/lib/src/version.dart +++ b/packages/flutter_tools/lib/src/version.dart @@ -105,7 +105,7 @@ class FlutterVersion { @override String toString() { final String versionText = frameworkVersion == 'unknown' ? '' : ' $frameworkVersion'; - final String flutterText = 'Flutter$versionText • channel $channel • ${repositoryUrl == null ? 'unknown source' : repositoryUrl}'; + final String flutterText = 'Flutter$versionText • channel $channel • ${repositoryUrl ?? 'unknown source'}'; final String frameworkText = 'Framework • revision $frameworkRevisionShort ($frameworkAge) • $frameworkCommitDate'; final String engineText = 'Engine • revision $engineRevisionShort'; final String toolsText = 'Tools • Dart $dartSdkVersion'; diff --git a/packages/flutter_tools/lib/src/vmservice.dart b/packages/flutter_tools/lib/src/vmservice.dart index 4290f6b978..6ba3677892 100644 --- a/packages/flutter_tools/lib/src/vmservice.dart +++ b/packages/flutter_tools/lib/src/vmservice.dart @@ -718,13 +718,11 @@ class VM extends ServiceObjectOwner { /// The number of bytes allocated (e.g. by malloc) in the native heap. int _heapAllocatedMemoryUsage; - int get heapAllocatedMemoryUsage { - return _heapAllocatedMemoryUsage == null ? 0 : _heapAllocatedMemoryUsage; - } + int get heapAllocatedMemoryUsage => _heapAllocatedMemoryUsage ?? 0; /// The peak resident set size for the process. int _maxRSS; - int get maxRSS => _maxRSS == null ? 0 : _maxRSS; + int get maxRSS => _maxRSS ?? 0; // The embedder's name, Flutter or dart_runner. String _embedder; diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 8e9e3a86da..4d2f5e7303 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -121,7 +121,7 @@ void testUsingContext( }, ); }); - }, timeout: timeout != null ? timeout : const Timeout(Duration(seconds: 60)), + }, timeout: timeout ?? const Timeout(Duration(seconds: 60)), testOn: testOn, skip: skip); }