Skip to content

Commit

Permalink
[various] Remove unnecessary null checks (flutter#4060)
Browse files Browse the repository at this point in the history
Removes `unnecessary_null_comparison: ignore` from the repository analysis options. Now that Dart 3 has reached the stable channel, all fully supported customers will be running in strong mode, so the extra null checks are no longer needed. While many packages do support earlier versions, this won't break anyone, it will just mean that people who are both a) using old versions of Flutter and b) violating the API contract by passing null to a non-nullable type, won't get error messages that are as obvious, which is fine for the best-effort support level we give to pre-current-stable versions of Flutter.
  • Loading branch information
stuartmorgan authored May 22, 2023
1 parent 30ebcf3 commit d449a17
Show file tree
Hide file tree
Showing 136 changed files with 242 additions and 396 deletions.
2 changes: 0 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ analyzer:
# allow self-reference to deprecated members (we do this because otherwise we have
# to annotate every member in every test, assert, etc, when we deprecate something)
deprecated_member_use_from_same_package: ignore
# Turned off until null-safe rollout is complete.
unnecessary_null_comparison: ignore
exclude: # DIFFERENT FROM FLUTTER/FLUTTER
# Ignore generated files
- '**/*.g.dart'
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.5+1

* Removes obsolete null checks on non-nullable values.

## 0.10.5

* Adds NV21 as an image streaming option for Android.
Expand Down
6 changes: 2 additions & 4 deletions packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
child: Center(
child: AspectRatio(
aspectRatio:
localVideoController.value.size != null
? localVideoController.value.aspectRatio
: 1.0,
localVideoController.value.aspectRatio,
child: VideoPlayer(localVideoController)),
),
),
Expand Down Expand Up @@ -1000,7 +998,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
: VideoPlayerController.file(File(videoFile!.path));

videoPlayerListener = () {
if (videoController != null && videoController!.value.size != null) {
if (videoController != null) {
// Refreshing the state to update video player with the correct ratio.
if (mounted) {
setState(() {});
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
Dart.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.10.5
version: 0.10.5+1

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.8+2

* Removes obsolete null checks on non-nullable values.

## 0.10.8+1

* Fixes lint errors.
Expand Down
6 changes: 2 additions & 4 deletions packages/camera/camera_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
child: Center(
child: AspectRatio(
aspectRatio:
localVideoController.value.size != null
? localVideoController.value.aspectRatio
: 1.0,
localVideoController.value.aspectRatio,
child: VideoPlayer(localVideoController)),
),
),
Expand Down Expand Up @@ -1008,7 +1006,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
: VideoPlayerController.file(File(videoFile!.path));

videoPlayerListener = () {
if (videoController != null && videoController!.value.size != null) {
if (videoController != null) {
// Refreshing the state to update video player with the correct ratio.
if (mounted) {
setState(() {});
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Android implementation of the camera plugin.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22

version: 0.10.8+1
version: 0.10.8+2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0+4

* Removes obsolete null checks on non-nullable values.

## 0.5.0+3

* Fixes Java lints.
Expand Down
6 changes: 2 additions & 4 deletions packages/camera/camera_android_camerax/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
child: Center(
child: AspectRatio(
aspectRatio:
localVideoController.value.size != null
? localVideoController.value.aspectRatio
: 1.0,
localVideoController.value.aspectRatio,
child: VideoPlayer(localVideoController)),
),
),
Expand Down Expand Up @@ -985,7 +983,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
: VideoPlayerController.file(File(videoFile!.path));

videoPlayerListener = () {
if (videoController != null && videoController!.value.size != null) {
if (videoController != null) {
// Refreshing the state to update video player with the correct ratio.
if (mounted) {
setState(() {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ class SystemServicesFlutterApiImpl implements SystemServicesFlutterApi {
void onDeviceOrientationChanged(String orientation) {
final DeviceOrientation deviceOrientation =
deserializeDeviceOrientation(orientation);
if (deviceOrientation == null) {
return;
}
SystemServices.deviceOrientationChangedStreamController
.add(DeviceOrientationChangedEvent(deviceOrientation));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android_camerax/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Android implementation of the camera plugin using the CameraX libra
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22

version: 0.5.0+3
version: 0.5.0+4

environment:
sdk: ">=2.19.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/camera/camera_avfoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.9.13+2

* Removes obsolete null checks on non-nullable values.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.

## 0.9.13+1
Expand Down
6 changes: 2 additions & 4 deletions packages/camera/camera_avfoundation/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
child: Center(
child: AspectRatio(
aspectRatio:
localVideoController.value.size != null
? localVideoController.value.aspectRatio
: 1.0,
localVideoController.value.aspectRatio,
child: VideoPlayer(localVideoController)),
),
),
Expand Down Expand Up @@ -1008,7 +1006,7 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
: VideoPlayerController.file(File(videoFile!.path));

videoPlayerListener = () {
if (videoController != null && videoController!.value.size != null) {
if (videoController != null) {
// Refreshing the state to update video player with the correct ratio.
if (mounted) {
setState(() {});
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_avfoundation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_avfoundation
description: iOS implementation of the camera plugin.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_avfoundation
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.9.13+1
version: 0.9.13+2

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/camera/camera_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.5.1

* Removes obsolete null checks on non-nullable values.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.

## 2.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class CameraEvent {
/// Build a Camera Event, that relates a `cameraId`.
///
/// The `cameraId` is the ID of the camera that triggered the event.
const CameraEvent(this.cameraId) : assert(cameraId != null);
const CameraEvent(this.cameraId);

/// The ID of the Camera this event is associated to.
final int cameraId;
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/camera/camera
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.5.0
version: 2.5.1

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/camera/camera_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.3.1+4

* Removes obsolete null checks on non-nullable values.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.

## 0.3.1+3
Expand Down
4 changes: 1 addition & 3 deletions packages/camera/camera_web/lib/src/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,7 @@ class Camera {
_onVideoRecordingErrorSubscription =
mediaRecorder!.onError.listen((html.Event event) {
final html.ErrorEvent error = event as html.ErrorEvent;
if (error != null) {
videoRecordingErrorController.add(error);
}
videoRecordingErrorController.add(error);
});

if (maxVideoDuration != null) {
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_web
description: A Flutter plugin for getting information about and controlling the camera on Web.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.3.1+3
version: 0.3.1+4

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/dynamic_layouts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.0.1+1

* Removes obsolete null checks on non-nullable values.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.
* Aligns Dart and Flutter SDK constraints.
* Updates minimum Flutter version to 3.0.
Expand Down
18 changes: 9 additions & 9 deletions packages/dynamic_layouts/lib/src/staggered_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class SliverGridStaggeredTileLayout extends DynamicSliverGridLayout {
required this.crossAxisSpacing,
required this.childCrossAxisExtent,
required this.scrollDirection,
}) : assert(crossAxisCount != null && crossAxisCount > 0),
assert(crossAxisSpacing != null && crossAxisSpacing >= 0),
assert(childCrossAxisExtent != null && childCrossAxisExtent >= 0);
}) : assert(crossAxisCount > 0),
assert(crossAxisSpacing >= 0),
assert(childCrossAxisExtent >= 0);

/// The number of children in the cross axis.
final int crossAxisCount;
Expand Down Expand Up @@ -200,9 +200,9 @@ class DynamicSliverGridDelegateWithFixedCrossAxisCount
required super.crossAxisCount,
super.mainAxisSpacing = 0.0,
super.crossAxisSpacing = 0.0,
}) : assert(crossAxisCount != null && crossAxisCount > 0),
assert(mainAxisSpacing != null && mainAxisSpacing >= 0),
assert(crossAxisSpacing != null && crossAxisSpacing >= 0);
}) : assert(crossAxisCount > 0),
assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0);

bool _debugAssertIsValid() {
assert(crossAxisCount > 0);
Expand Down Expand Up @@ -283,9 +283,9 @@ class DynamicSliverGridDelegateWithMaxCrossAxisExtent
required super.maxCrossAxisExtent,
super.mainAxisSpacing = 0.0,
super.crossAxisSpacing = 0.0,
}) : assert(maxCrossAxisExtent != null && maxCrossAxisExtent > 0),
assert(mainAxisSpacing != null && mainAxisSpacing >= 0),
assert(crossAxisSpacing != null && crossAxisSpacing >= 0);
}) : assert(maxCrossAxisExtent > 0),
assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0);

bool _debugAssertIsValid(double crossAxisExtent) {
assert(crossAxisExtent > 0.0);
Expand Down
19 changes: 9 additions & 10 deletions packages/dynamic_layouts/lib/src/wrap_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ class SliverGridWrappingTileLayout extends DynamicSliverGridLayout {
required this.childCrossAxisExtent,
required this.crossAxisExtent,
required this.scrollDirection,
}) : assert(mainAxisSpacing != null && mainAxisSpacing >= 0),
assert(crossAxisSpacing != null && crossAxisSpacing >= 0),
assert(childMainAxisExtent != null && childMainAxisExtent >= 0),
assert(childCrossAxisExtent != null && childCrossAxisExtent >= 0),
assert(crossAxisExtent != null && crossAxisExtent >= 0),
assert(scrollDirection != null &&
(scrollDirection == Axis.horizontal ||
scrollDirection == Axis.vertical));
}) : assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0),
assert(childMainAxisExtent >= 0),
assert(childCrossAxisExtent >= 0),
assert(crossAxisExtent >= 0),
assert(scrollDirection == Axis.horizontal ||
scrollDirection == Axis.vertical);

/// The direction in which the layout should be built.
final Axis scrollDirection;
Expand Down Expand Up @@ -216,8 +215,8 @@ class SliverGridDelegateWithWrapping extends SliverGridDelegate {
this.crossAxisSpacing = 0.0,
this.childCrossAxisExtent = double.infinity,
this.childMainAxisExtent = double.infinity,
}) : assert(mainAxisSpacing != null && mainAxisSpacing >= 0),
assert(crossAxisSpacing != null && crossAxisSpacing >= 0);
}) : assert(mainAxisSpacing >= 0),
assert(crossAxisSpacing >= 0);

/// The number of pixels from the leading edge of one tile to the trailing
/// edge of the same tile in the main axis.
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamic_layouts/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dynamic_layouts
description: Widgets for building dynamic grid layouts.
version: 0.0.1
version: 0.0.1+1
issue_tracker: https://github.com/flutter/flutter/labels/p%3A%20dynamic_layouts
repository: https://github.com/flutter/packages/tree/main/packages/dynamic_layouts
# Temporarily unpublished while in process of releasing full package in multiple stages.
Expand Down
3 changes: 0 additions & 3 deletions packages/flutter_markdown/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ class TestAssetBundle extends CachingAssetBundle {
io.File('${rootDirectory.path}/test/assets/images/logo.png');

final ByteData asset = ByteData.view(file.readAsBytesSync().buffer);
if (asset == null) {
throw FlutterError('Unable to load asset: $key');
}
return asset;
} else {
throw ArgumentError('Unknown asset key: $key');
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_migrate/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.1+3

* Removes obsolete null checks on non-nullable values.

## 0.0.1+2

* Removes use of `runtimeType.toString()`.
Expand Down
6 changes: 2 additions & 4 deletions packages/flutter_migrate/lib/src/base/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ class BufferLogger extends Logger {
String? progressId,
int progressIndicatorPadding = kDefaultStatusPadding,
}) {
assert(progressIndicatorPadding != null);
printStatus(message);
return SilentStatus(
stopwatch: _stopwatchFactory.createStopwatch(),
Expand Down Expand Up @@ -983,7 +982,6 @@ class AnonymousSpinnerStatus extends Status {

void _callback(Timer timer) {
assert(this.timer == timer);
assert(timer != null);
assert(timer.isActive);
_writeToStdOut(_backspaceChar * _lastAnimationFrameLength);
ticks += 1;
Expand Down Expand Up @@ -1147,7 +1145,7 @@ String wrapText(
int? indent,
}) {
assert(columnWidth >= 0);
if (text == null || text.isEmpty) {
if (text.isEmpty) {
return '';
}
indent ??= 0;
Expand Down Expand Up @@ -1231,7 +1229,7 @@ List<String> _wrapTextAsLines(
required int columnWidth,
required bool shouldWrap,
}) {
if (text == null || text.isEmpty) {
if (text.isEmpty) {
return <String>[''];
}
assert(start >= 0);
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter_migrate/lib/src/base/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class FlutterProjectFactory {
/// Returns a [FlutterProject] view of the given directory or a ToolExit error,
/// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
FlutterProject fromDirectory(Directory directory) {
assert(directory != null);
return projects.putIfAbsent(directory.path, () {
return FlutterProject(directory);
});
Expand All @@ -36,7 +35,7 @@ class FlutterProjectFactory {

/// Represents the contents of a Flutter project at the specified [directory].
class FlutterProject {
FlutterProject(this.directory) : assert(directory != null);
FlutterProject(this.directory);

/// Returns a [FlutterProject] view of the current directory or a ToolExit error,
/// if `pubspec.yaml` or `example/pubspec.yaml` is invalid.
Expand Down
4 changes: 1 addition & 3 deletions packages/flutter_migrate/lib/src/base/terminal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ class AnsiTerminal implements Terminal {

@override
String bolden(String message) {
assert(message != null);
if (!supportsColor || message.isEmpty) {
return message;
}
Expand All @@ -256,8 +255,7 @@ class AnsiTerminal implements Terminal {

@override
String color(String message, TerminalColor color) {
assert(message != null);
if (!supportsColor || color == null || message.isEmpty) {
if (!supportsColor || message.isEmpty) {
return message;
}
final StringBuffer buffer = StringBuffer();
Expand Down
Loading

0 comments on commit d449a17

Please sign in to comment.