Implementing null-aware operators throughout the repository (#143804)

This pull request fixes #143803 by taking advantage of Dart's null-aware operators.

And unlike `switch` expressions ([9 PRs](https://github.com/flutter/flutter/pull/143634) and counting), the Flutter codebase is already fantastic when it comes to null-aware coding. After refactoring the entire repo, all the changes involving `?.` and `??` can fit into a single pull request.
This commit is contained in:
Nate
2024-02-23 12:02:22 -07:00
committed by GitHub
parent 39585e66c1
commit c53a18f4e4
55 changed files with 93 additions and 365 deletions

View File

@@ -521,11 +521,7 @@ class AndroidSdk {
final String executable = globals.platform.isWindows
? 'sdkmanager.bat'
: 'sdkmanager';
final String? path = getCmdlineToolsPath(executable, skipOldTools: true);
if (path != null) {
return path;
}
return null;
return getCmdlineToolsPath(executable, skipOldTools: true);
}
/// Returns the version of the Android SDK manager tool or null if not found.

View File

@@ -163,11 +163,7 @@ class Java {
RegExp(r'java\s+(?<version>\d+(\.\d+)?(\.\d+)?)\s+\d\d\d\d-\d\d-\d\d');
final RegExpMatch? match = secondJdkVersionRegex.firstMatch(versionLines[0]);
if (match != null) {
final Version? parsedVersion = Version.parse(match.namedGroup('version'));
if (parsedVersion == null) {
return null;
}
return parsedVersion;
return Version.parse(match.namedGroup('version'));
}
_logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
return null;

View File

@@ -557,11 +557,9 @@ class Cache {
/// Return the top-level directory in the cache; this is `bin/cache`.
Directory getRoot() {
if (_rootOverride != null) {
return _fileSystem.directory(_fileSystem.path.join(_rootOverride.path, 'bin', 'cache'));
} else {
return _fileSystem.directory(_fileSystem.path.join(flutterRoot!, 'bin', 'cache'));
}
return _fileSystem.directory(
_fileSystem.path.join(_rootOverride?.path ?? flutterRoot!, 'bin', 'cache'),
);
}
String getHostPlatformArchName() {

View File

@@ -84,10 +84,7 @@ abstract class AnalyzeBase {
bool get isFlutterRepo => argResults['flutter-repo'] as bool;
String get sdkPath {
final String? dartSdk = argResults['dart-sdk'] as String?;
if (dartSdk != null) {
return dartSdk;
}
return artifacts.getArtifactPath(Artifact.engineDartSdkPath);
return dartSdk ?? artifacts.getArtifactPath(Artifact.engineDartSdkPath);
}
bool get isBenchmarking => argResults['benchmark'] as bool;
String? get protocolTrafficLog => argResults['protocol-traffic-log'] as String?;

View File

@@ -399,14 +399,7 @@ class PackagesGetCommand extends FlutterCommand {
return findPlugins(rootProject, throwOnError: false);
})();
late final String? _androidEmbeddingVersion = (() {
final FlutterProject? rootProject = _rootProject;
if (rootProject == null) {
return null;
}
return rootProject.android.getEmbeddingVersion().toString().split('.').last;
})();
late final String? _androidEmbeddingVersion = _rootProject?.android.getEmbeddingVersion().toString().split('.').last;
/// The pub packages usage values are incorrect since these are calculated/sent
/// before pub get completes. This needs to be performed after dependency resolution.

View File

@@ -224,10 +224,7 @@ ${migrateConfig.getOutputFileString()}''';
/// Finds the fallback revision to use when no base revision is found in the migrate config.
String getFallbackBaseRevision(Logger logger, FlutterVersion flutterVersion) {
// Use the .metadata file if it exists.
if (versionRevision != null) {
return versionRevision!;
}
return flutterVersion.frameworkRevision;
return versionRevision ?? flutterVersion.frameworkRevision;
}
}

View File

@@ -1178,12 +1178,7 @@ class IOSDeviceLogReader extends DeviceLogReader {
bool isWirelesslyConnected = false,
bool isCoreDevice = false,
}) {
final int sdkVersion;
if (majorSdkVersion != null) {
sdkVersion = majorSdkVersion;
} else {
sdkVersion = useSyslog ? 12 : 13;
}
final int sdkVersion = majorSdkVersion ?? (useSyslog ? 12 : 13);
return IOSDeviceLogReader._(
iMobileDevice, sdkVersion, '1234', 'test', isWirelesslyConnected, isCoreDevice, 'Runner', usingCISystem);
}

View File

@@ -637,12 +637,7 @@ class IOSDeployDebugger {
);
}
if (_stdinWriteFuture != null) {
_stdinWriteFuture = _stdinWriteFuture!.then<void>((_) => writeln());
} else {
_stdinWriteFuture = writeln();
}
_stdinWriteFuture = _stdinWriteFuture?.then<void>((_) => writeln()) ?? writeln();
return _stdinWriteFuture;
}

View File

@@ -847,12 +847,7 @@ void _parseIssueInStdout(XcodeBuildExecution xcodeBuildExecution, Logger logger,
String? _parseMissingPlatform(String message) {
final RegExp pattern = RegExp(r'error:(.*?) is not installed\. To use with Xcode, first download and install the platform');
final RegExpMatch? match = pattern.firstMatch(message);
if (match != null) {
final String? version = match.group(1);
return version;
}
return null;
return pattern.firstMatch(message)?.group(1);
}
// The result of [_handleXCResultIssue].

View File

@@ -717,11 +717,7 @@ class VersionUpstreamValidator {
// URLs without ".git" suffix will remain unaffected.
static final RegExp _patternUrlDotGit = RegExp(r'(.*)(\.git)$');
static String stripDotGit(String url) {
final RegExpMatch? match = _patternUrlDotGit.firstMatch(url);
if (match == null) {
return url;
}
return match.group(1)!;
return _patternUrlDotGit.firstMatch(url)?.group(1)! ?? url;
}
}

View File

@@ -329,10 +329,7 @@ class IosProject extends XcodeBasedProject {
Future<String?> _getTeamIdentifier(XcodeProjectBuildContext buildContext) async {
final Map<String, String>? buildSettings = await _buildSettingsForXcodeProjectBuildContext(buildContext);
if (buildSettings != null) {
return buildSettings[kTeamIdKey];
}
return null;
return buildSettings?[kTeamIdKey];
}
Future<List<String>> _getAssociatedDomains(XcodeProjectBuildContext buildContext) async {

View File

@@ -1130,10 +1130,7 @@ class TestDeviceDiscoverySupportFilter extends DeviceDiscoverySupportFilter {
@override
bool isDeviceSupportedForProject(Device device) {
if (isAlwaysSupportedForProjectOverride != null) {
return isAlwaysSupportedForProjectOverride!;
}
return super.isDeviceSupportedForProject(device);
return isAlwaysSupportedForProjectOverride ?? super.isDeviceSupportedForProject(device);
}
}

View File

@@ -24,90 +24,60 @@ class FlutterIOOverrides extends io.IOOverrides {
@override
io.Directory createDirectory(String path) {
if (_fileSystemDelegate == null) {
return super.createDirectory(path);
}
return _fileSystemDelegate.directory(path);
return _fileSystemDelegate?.directory(path) ?? super.createDirectory(path);
}
@override
io.File createFile(String path) {
if (_fileSystemDelegate == null) {
return super.createFile(path);
}
return _fileSystemDelegate.file(path);
return _fileSystemDelegate?.file(path) ?? super.createFile(path);
}
@override
io.Link createLink(String path) {
if (_fileSystemDelegate == null) {
return super.createLink(path);
}
return _fileSystemDelegate.link(path);
return _fileSystemDelegate?.link(path) ?? super.createLink(path);
}
@override
Stream<FileSystemEvent> fsWatch(String path, int events, bool recursive) {
if (_fileSystemDelegate == null) {
return super.fsWatch(path, events, recursive);
}
return _fileSystemDelegate.file(path).watch(events: events, recursive: recursive);
return _fileSystemDelegate?.file(path).watch(events: events, recursive: recursive)
?? super.fsWatch(path, events, recursive);
}
@override
bool fsWatchIsSupported() {
if (_fileSystemDelegate == null) {
return super.fsWatchIsSupported();
}
return _fileSystemDelegate.isWatchSupported;
return _fileSystemDelegate?.isWatchSupported ?? super.fsWatchIsSupported();
}
@override
Future<FileSystemEntityType> fseGetType(String path, bool followLinks) {
if (_fileSystemDelegate == null) {
return super.fseGetType(path, followLinks);
}
return _fileSystemDelegate.type(path, followLinks: followLinks);
return _fileSystemDelegate?.type(path, followLinks: followLinks)
?? super.fseGetType(path, followLinks);
}
@override
FileSystemEntityType fseGetTypeSync(String path, bool followLinks) {
if (_fileSystemDelegate == null) {
return super.fseGetTypeSync(path, followLinks);
}
return _fileSystemDelegate.typeSync(path, followLinks: followLinks);
return _fileSystemDelegate?.typeSync(path, followLinks: followLinks)
?? super.fseGetTypeSync(path, followLinks);
}
@override
Future<bool> fseIdentical(String path1, String path2) {
if (_fileSystemDelegate == null) {
return super.fseIdentical(path1, path2);
}
return _fileSystemDelegate.identical(path1, path2);
return _fileSystemDelegate?.identical(path1, path2) ?? super.fseIdentical(path1, path2);
}
@override
bool fseIdenticalSync(String path1, String path2) {
if (_fileSystemDelegate == null) {
return super.fseIdenticalSync(path1, path2);
}
return _fileSystemDelegate.identicalSync(path1, path2);
return _fileSystemDelegate?.identicalSync(path1, path2) ?? super.fseIdenticalSync(path1, path2);
}
@override
io.Directory getCurrentDirectory() {
if (_fileSystemDelegate == null) {
return super.getCurrentDirectory();
}
return _fileSystemDelegate.currentDirectory;
return _fileSystemDelegate?.currentDirectory ?? super.getCurrentDirectory();
}
@override
io.Directory getSystemTempDirectory() {
if (_fileSystemDelegate == null) {
return super.getSystemTempDirectory();
}
return _fileSystemDelegate.systemTempDirectory;
return _fileSystemDelegate?.systemTempDirectory ?? super.getSystemTempDirectory();
}
@override
@@ -120,17 +90,11 @@ class FlutterIOOverrides extends io.IOOverrides {
@override
Future<FileStat> stat(String path) {
if (_fileSystemDelegate == null) {
return super.stat(path);
}
return _fileSystemDelegate.stat(path);
return _fileSystemDelegate?.stat(path) ?? super.stat(path);
}
@override
FileStat statSync(String path) {
if (_fileSystemDelegate == null) {
return super.statSync(path);
}
return _fileSystemDelegate.statSync(path);
return _fileSystemDelegate?.statSync(path) ?? super.statSync(path);
}
}