From 60f5ae8e77ecfd479333ce68a8fd81b0778132f1 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 9 May 2017 21:15:42 -0700 Subject: [PATCH] Add const non-null asserts where required (#9945) Also includes minor doc fixes. --- dev/benchmarks/complex_layout/lib/main.dart | 12 +++++----- .../lib/demo/material/drawer_demo.dart | 14 +++++------ .../lib/src/foundation/assertions.dart | 2 +- packages/flutter/lib/src/rendering/box.dart | 4 +++- packages/flutter/lib/src/rendering/flex.dart | 1 + .../flutter/lib/src/rendering/proxy_box.dart | 3 ++- .../flutter/lib/src/rendering/sliver.dart | 21 +++++++++++++--- .../lib/src/rendering/sliver_grid.dart | 22 ++++++++++++----- packages/flutter/lib/src/rendering/table.dart | 6 ++--- .../lib/src/services/image_provider.dart | 24 ++++++++++++------- .../lib/src/services/image_resolution.dart | 6 +---- .../lib/src/services/image_stream.dart | 7 +++--- .../lib/src/services/raw_keyboard.dart | 14 +++++++---- .../lib/src/services/text_editing.dart | 8 +++++-- 14 files changed, 92 insertions(+), 52 deletions(-) diff --git a/dev/benchmarks/complex_layout/lib/main.dart b/dev/benchmarks/complex_layout/lib/main.dart index 4baadca999..1d78c37ffd 100644 --- a/dev/benchmarks/complex_layout/lib/main.dart +++ b/dev/benchmarks/complex_layout/lib/main.dart @@ -349,10 +349,10 @@ class UserHeader extends StatelessWidget { child: new Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ - new Padding( + const Padding( padding: const EdgeInsets.only(right: 8.0), - child: new Image( - image: new AssetImage('packages/flutter_gallery_assets/ali_connors_sml.png'), + child: const Image( + image: const AssetImage('packages/flutter_gallery_assets/ali_connors_sml.png'), width: 32.0, height: 32.0 ) @@ -407,10 +407,10 @@ class ItemImageBox extends StatelessWidget { children: [ new Stack( children: [ - new SizedBox( + const SizedBox( height: 230.0, - child: new Image( - image: new AssetImage('packages/flutter_gallery_assets/top_10_australian_beaches.png') + child: const Image( + image: const AssetImage('packages/flutter_gallery_assets/top_10_australian_beaches.png') ) ), new Theme( diff --git a/examples/flutter_gallery/lib/demo/material/drawer_demo.dart b/examples/flutter_gallery/lib/demo/material/drawer_demo.dart index d0ae9ffc4b..5840de36aa 100644 --- a/examples/flutter_gallery/lib/demo/material/drawer_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/drawer_demo.dart @@ -93,10 +93,10 @@ class _DrawerDemoState extends State with TickerProviderStateMixin { new UserAccountsDrawerHeader( accountName: const Text('Zach Widget'), accountEmail: const Text('zach.widget@example.com'), - currentAccountPicture: new CircleAvatar(backgroundImage: new AssetImage(_kAsset0)), - otherAccountsPictures: [ - new CircleAvatar(backgroundImage: new AssetImage(_kAsset1)), - new CircleAvatar(backgroundImage: new AssetImage(_kAsset2)), + currentAccountPicture: const CircleAvatar(backgroundImage: const AssetImage(_kAsset0)), + otherAccountsPictures: const [ + const CircleAvatar(backgroundImage: const AssetImage(_kAsset1)), + const CircleAvatar(backgroundImage: const AssetImage(_kAsset2)), ], onDetailsPressed: () { _showDrawerContents = !_showDrawerContents; @@ -164,10 +164,10 @@ class _DrawerDemoState extends State with TickerProviderStateMixin { new Container( width: 100.0, height: 100.0, - decoration: new BoxDecoration( + decoration: const BoxDecoration( shape: BoxShape.circle, - image: new DecorationImage( - image: new AssetImage(_kAsset0), + image: const DecorationImage( + image: const AssetImage(_kAsset0), ), ), ), diff --git a/packages/flutter/lib/src/foundation/assertions.dart b/packages/flutter/lib/src/foundation/assertions.dart index 71bc12c086..ad62acd946 100644 --- a/packages/flutter/lib/src/foundation/assertions.dart +++ b/packages/flutter/lib/src/foundation/assertions.dart @@ -33,7 +33,7 @@ class FlutterErrorDetails { this.stackFilter, this.informationCollector, this.silent: false - }) : assert(exception != null); + }); /// The exception. Often this will be an [AssertionError], maybe specifically /// a [FlutterError]. However, this could be any value at all. diff --git a/packages/flutter/lib/src/rendering/box.dart b/packages/flutter/lib/src/rendering/box.dart index 07fbd35a7e..06a78449d7 100644 --- a/packages/flutter/lib/src/rendering/box.dart +++ b/packages/flutter/lib/src/rendering/box.dart @@ -504,7 +504,9 @@ class BoxHitTestEntry extends HitTestEntry { /// Creates a box hit test entry. /// /// The [localPosition] argument must not be null. - const BoxHitTestEntry(RenderBox target, this.localPosition) : super(target); + const BoxHitTestEntry(RenderBox target, this.localPosition) + : assert(localPosition != null), + super(target); @override RenderBox get target => super.target; diff --git a/packages/flutter/lib/src/rendering/flex.dart b/packages/flutter/lib/src/rendering/flex.dart index 6de086a267..e12d19cb4e 100644 --- a/packages/flutter/lib/src/rendering/flex.dart +++ b/packages/flutter/lib/src/rendering/flex.dart @@ -229,6 +229,7 @@ class RenderFlex extends RenderBox with ContainerRenderObjectMixin _textBaseline; TextBaseline _textBaseline; set textBaseline(TextBaseline value) { + assert(_crossAxisAlignment != CrossAxisAlignment.baseline || value != null); if (_textBaseline != value) { _textBaseline = value; markNeedsLayout(); diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 01388ef410..42184b710c 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -2627,7 +2627,8 @@ class RenderSemanticsGestureHandler extends RenderProxyBox implements SemanticsA GestureDragUpdateCallback onHorizontalDragUpdate, GestureDragUpdateCallback onVerticalDragUpdate, this.scrollFactor: 0.8 - }) : _onTap = onTap, + }) : assert(scrollFactor != null), + _onTap = onTap, _onLongPress = onLongPress, _onHorizontalDragUpdate = onHorizontalDragUpdate, _onVerticalDragUpdate = onVerticalDragUpdate, diff --git a/packages/flutter/lib/src/rendering/sliver.dart b/packages/flutter/lib/src/rendering/sliver.dart index 6d506f0886..9b879046ad 100644 --- a/packages/flutter/lib/src/rendering/sliver.dart +++ b/packages/flutter/lib/src/rendering/sliver.dart @@ -189,7 +189,14 @@ class SliverConstraints extends Constraints { @required this.remainingPaintExtent, @required this.crossAxisExtent, @required this.viewportMainAxisExtent, - }); + }) : assert(axisDirection != null), + assert(growthDirection != null), + assert(userScrollDirection != null), + assert(scrollOffset != null), + assert(overlap != null), + assert(remainingPaintExtent != null), + assert(crossAxisExtent != null), + assert(viewportMainAxisExtent != null); /// Creates a copy of this object but with the given fields replaced with the /// new values. @@ -469,7 +476,13 @@ class SliverGeometry { bool visible, this.hasVisualOverflow: false, this.scrollOffsetCorrection: 0.0 - }) : layoutExtent = layoutExtent ?? paintExtent, + }) : assert(scrollExtent != null), + assert(paintExtent != null), + assert(paintOrigin != null), + assert(maxPaintExtent != null), + assert(hasVisualOverflow != null), + assert(scrollOffsetCorrection != null), + layoutExtent = layoutExtent ?? paintExtent, hitTestExtent = hitTestExtent ?? paintExtent, visible = visible ?? paintExtent > 0.0; @@ -635,7 +648,9 @@ class SliverHitTestEntry extends HitTestEntry { const SliverHitTestEntry(RenderSliver target, { @required this.mainAxisPosition, @required this.crossAxisPosition, - }) : super(target); + }) : assert(mainAxisPosition != null), + assert(crossAxisPosition != null), + super(target); @override RenderSliver get target => super.target; diff --git a/packages/flutter/lib/src/rendering/sliver_grid.dart b/packages/flutter/lib/src/rendering/sliver_grid.dart index 9771b34d4c..039ca12b70 100644 --- a/packages/flutter/lib/src/rendering/sliver_grid.dart +++ b/packages/flutter/lib/src/rendering/sliver_grid.dart @@ -151,7 +151,11 @@ class SliverGridRegularTileLayout extends SliverGridLayout { @required this.crossAxisStride, @required this.childMainAxisExtent, @required this.childCrossAxisExtent, - }); + }) : assert(crossAxisCount != null && crossAxisCount > 0), + assert(mainAxisStride != null && mainAxisStride >= 0), + assert(crossAxisStride != null && crossAxisStride >= 0), + assert(childMainAxisExtent != null && childMainAxisExtent >= 0), + assert(childCrossAxisExtent != null && childCrossAxisExtent >= 0); /// The number of children in the cross axis. final int crossAxisCount; @@ -272,7 +276,10 @@ class SliverGridDelegateWithFixedCrossAxisCount extends SliverGridDelegate { this.mainAxisSpacing: 0.0, this.crossAxisSpacing: 0.0, this.childAspectRatio: 1.0, - }); + }) : assert(crossAxisCount != null && crossAxisCount > 0), + assert(mainAxisSpacing != null && mainAxisSpacing >= 0), + assert(crossAxisSpacing != null && crossAxisSpacing >= 0), + assert(childAspectRatio != null && childAspectRatio > 0); /// The number of children in the cross axis. final int crossAxisCount; @@ -347,15 +354,18 @@ class SliverGridDelegateWithMaxCrossAxisExtent extends SliverGridDelegate { /// Creates a delegate that makes grid layouts with tiles that have a maximum /// cross-axis extent. /// - /// All of the arguments must not be null. The `maxCrossAxisExtent` and - /// `crossAxisSpacing` arguments must not be negative. The `crossAxisCount` - /// and `childAspectRatio` arguments must be greater than zero. + /// All of the arguments must not be null. The [maxCrossAxisExtent] and + /// [mainAxisSpacing], and [crossAxisSpacing] arguments must not be negative. + /// The [childAspectRatio] argument must be greater than zero. const SliverGridDelegateWithMaxCrossAxisExtent({ @required this.maxCrossAxisExtent, this.mainAxisSpacing: 0.0, this.crossAxisSpacing: 0.0, this.childAspectRatio: 1.0, - }); + }) : assert(maxCrossAxisExtent != null && maxCrossAxisExtent >= 0), + assert(mainAxisSpacing != null && mainAxisSpacing >= 0), + assert(crossAxisSpacing != null && crossAxisSpacing >= 0), + assert(childAspectRatio != null && childAspectRatio > 0); /// The maximum extent of tiles in the cross axis. /// diff --git a/packages/flutter/lib/src/rendering/table.dart b/packages/flutter/lib/src/rendering/table.dart index 33cee79816..82153716bf 100644 --- a/packages/flutter/lib/src/rendering/table.dart +++ b/packages/flutter/lib/src/rendering/table.dart @@ -121,7 +121,7 @@ class FixedColumnWidth extends TableColumnWidth { /// Creates a column width based on a fixed number of logical pixels. /// /// The [value] argument must not be null. - const FixedColumnWidth(this.value); + const FixedColumnWidth(this.value) : assert(value != null); /// The width the column should occupy in logical pixels. final double value; @@ -148,7 +148,7 @@ class FractionColumnWidth extends TableColumnWidth { /// maxWidth. /// /// The [value] argument must not be null. - const FractionColumnWidth(this.value); + const FractionColumnWidth(this.value) : assert(value != null); /// The fraction of the table's constraints' maxWidth that this column should /// occupy. @@ -184,7 +184,7 @@ class FlexColumnWidth extends TableColumnWidth { /// the other columns have been laid out. /// /// The [value] argument must not be null. - const FlexColumnWidth([this.value = 1.0]); + const FlexColumnWidth([this.value = 1.0]) : assert(value != null); /// The reaction of the of the remaining space once all the other columns have /// been laid out that this column should occupy. diff --git a/packages/flutter/lib/src/services/image_provider.dart b/packages/flutter/lib/src/services/image_provider.dart index a15a4c2f93..306435b7be 100644 --- a/packages/flutter/lib/src/services/image_provider.dart +++ b/packages/flutter/lib/src/services/image_provider.dart @@ -223,7 +223,9 @@ class AssetBundleImageKey { @required this.bundle, @required this.name, @required this.scale - }); + }) : assert(bundle != null), + assert(name != null), + assert(scale != null); /// The bundle from which the image will be obtained. /// @@ -312,7 +314,9 @@ class NetworkImage extends ImageProvider { /// Creates an object that fetches the image at the given URL. /// /// The arguments must not be null. - const NetworkImage(this.url, { this.scale: 1.0 }) : assert(url != null); + const NetworkImage(this.url, { this.scale: 1.0 }) + : assert(url != null), + assert(scale != null); /// The URL from which the image will be fetched. final String url; @@ -382,7 +386,9 @@ class FileImage extends ImageProvider { /// Creates an object that decodes a [File] as an image. /// /// The arguments must not be null. - const FileImage(this.file, { this.scale: 1.0 }); + const FileImage(this.file, { this.scale: 1.0 }) + : assert(file != null), + assert(scale != null); /// The file to decode into an image. final File file; @@ -444,7 +450,9 @@ class MemoryImage extends ImageProvider { /// Creates an object that decodes a [Uint8List] buffer as an image. /// /// The arguments must not be null. - const MemoryImage(this.bytes, { this.scale: 1.0 }); + const MemoryImage(this.bytes, { this.scale: 1.0 }) + : assert(bytes != null), + assert(scale != null); /// The bytes to decode into an image. final Uint8List bytes; @@ -504,13 +512,11 @@ class ExactAssetImage extends AssetBundleImageProvider { /// defaults to 1.0. The [bundle] argument may be null, in which case the /// bundle provided in the [ImageConfiguration] passed to the [resolve] call /// will be used instead. - ExactAssetImage(this.name, { + const ExactAssetImage(this.name, { this.scale: 1.0, this.bundle - }) { - assert(name != null); - assert(scale != null); - } + }) : assert(name != null), + assert(scale != null); /// The key to use to obtain the resource from the [bundle]. This is the /// argument passed to [AssetBundle.load]. diff --git a/packages/flutter/lib/src/services/image_resolution.dart b/packages/flutter/lib/src/services/image_resolution.dart index 123860e7c3..25f5da8d48 100644 --- a/packages/flutter/lib/src/services/image_resolution.dart +++ b/packages/flutter/lib/src/services/image_resolution.dart @@ -60,11 +60,7 @@ class AssetImage extends AssetBundleImageProvider { /// /// The [name] argument must not be null. It should name the main asset from /// the set of images to chose from. - AssetImage(this.name, { - this.bundle - }) { - assert(name != null); - } + const AssetImage(this.name, { this.bundle }) : assert(name != null); /// The name of the main asset from the set of images to chose from. See the /// documentation for the [AssetImage] class itself for details. diff --git a/packages/flutter/lib/src/services/image_stream.dart b/packages/flutter/lib/src/services/image_stream.dart index 4f0c8ecfbf..baa013e046 100644 --- a/packages/flutter/lib/src/services/image_stream.dart +++ b/packages/flutter/lib/src/services/image_stream.dart @@ -16,10 +16,9 @@ class ImageInfo { /// Creates an [ImageInfo] object for the given image and scale. /// /// Both the image and the scale must not be null. - ImageInfo({ @required this.image, this.scale: 1.0 }) { - assert(image != null); - assert(scale != null); - } + const ImageInfo({ @required this.image, this.scale: 1.0 }) + : assert(image != null), + assert(scale != null); /// The raw image pixels. /// diff --git a/packages/flutter/lib/src/services/raw_keyboard.dart b/packages/flutter/lib/src/services/raw_keyboard.dart index f77449ac5f..11330ea2fc 100644 --- a/packages/flutter/lib/src/services/raw_keyboard.dart +++ b/packages/flutter/lib/src/services/raw_keyboard.dart @@ -42,7 +42,11 @@ class RawKeyEventDataAndroid extends RawKeyEventData { this.keyCode: 0, this.scanCode: 0, this.metaState: 0, - }); + }) : assert(flags != null), + assert(codePoint != null), + assert(keyCode != null), + assert(scanCode != null), + assert(metaState != null); /// See final int flags; @@ -67,12 +71,14 @@ class RawKeyEventDataAndroid extends RawKeyEventData { class RawKeyEventDataFuchsia extends RawKeyEventData { /// Creates a key event data structure specific for Android. /// - /// The [hidUsage] and [codePoint] arguments must not be null. + /// The [hidUsage], [codePoint], and [modifiers] arguments must not be null. const RawKeyEventDataFuchsia({ this.hidUsage: 0, this.codePoint: 0, this.modifiers: 0, - }); + }) : assert(hidUsage != null), + assert(codePoint != null), + assert(modifiers != null); /// The USB HID usage. /// @@ -84,7 +90,7 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { /// If there is no Unicode code point, this value is zero. final int codePoint; - /// The modifiers that we present when the key event occured. + /// The modifiers that we present when the key event occurred. /// /// See /// for the numerical values of the modifiers. diff --git a/packages/flutter/lib/src/services/text_editing.dart b/packages/flutter/lib/src/services/text_editing.dart index bfe1d96abb..f84cce677c 100644 --- a/packages/flutter/lib/src/services/text_editing.dart +++ b/packages/flutter/lib/src/services/text_editing.dart @@ -21,11 +21,15 @@ class TextRange { const TextRange({ @required this.start, @required this.end - }); + }) : assert(start != null && start >= -1), + assert(end != null && end >= -1); /// A text range that starts and ends at offset. + /// + /// The [offset] argument must be non-null and greater than or equal to -1. const TextRange.collapsed(int offset) - : start = offset, + : assert(offset != null && offset >= -1), + start = offset, end = offset; /// A text range that contains nothing and is not in the text.