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

@@ -91,11 +91,7 @@ Iterable<InlineSpanSemanticsInformation> combineSemanticsInfoSyncStar(List<Inlin
workingText += info.text;
workingLabel ??= '';
final String? infoSemanticsLabel = info.semanticsLabel;
if (infoSemanticsLabel != null) {
workingLabel += infoSemanticsLabel;
} else {
workingLabel += info.text;
}
workingLabel += infoSemanticsLabel ?? info.text;
}
}
assert(workingLabel != null);
@@ -115,11 +111,7 @@ Iterable<InlineSpanSemanticsInformation> combineSemanticsInfoList(List<InlineSpa
workingText += info.text;
workingLabel ??= '';
final String? infoSemanticsLabel = info.semanticsLabel;
if (infoSemanticsLabel != null) {
workingLabel += infoSemanticsLabel;
} else {
workingLabel += info.text;
}
workingLabel += infoSemanticsLabel ?? info.text;
}
}
assert(workingLabel != null);

View File

@@ -16,20 +16,13 @@ class TestSpecs {
int startTime;
int? _endTime;
int get milliseconds {
return endTime - startTime;
}
int get milliseconds => endTime - startTime;
set endTime(int value) {
_endTime = value;
}
int get endTime {
if (_endTime == null) {
return 0;
}
return _endTime!;
}
int get endTime => _endTime ?? 0;
String toJson() {
return json.encode(

View File

@@ -158,13 +158,10 @@ class Version {
int nextZ = previousVersion.z;
int? nextM = previousVersion.m;
int? nextN = previousVersion.n;
if (nextVersionType == null) {
if (previousVersion.type == VersionType.latest || previousVersion.type == VersionType.gitDescribe) {
nextVersionType = VersionType.development;
} else {
nextVersionType = previousVersion.type;
}
}
nextVersionType ??= switch (previousVersion.type) {
VersionType.stable => VersionType.stable,
VersionType.latest || VersionType.gitDescribe || VersionType.development => VersionType.development,
};
switch (increment) {
case 'x':

View File

@@ -32,11 +32,7 @@ Future<void> main() async {
final TaskResult? firstInstallFailure = testResults
.firstWhereOrNull((TaskResult element) => element.failed);
if (firstInstallFailure != null) {
return firstInstallFailure;
}
return TaskResult.success(null);
return firstInstallFailure ?? TaskResult.success(null);
},
);

View File

@@ -32,11 +32,7 @@ Future<void> main() async {
final TaskResult? firstInstallFailure = testResults
.firstWhereOrNull((TaskResult element) => element.failed);
if (firstInstallFailure != null) {
return firstInstallFailure;
}
return TaskResult.success(null);
return firstInstallFailure ?? TaskResult.success(null);
},
);

View File

@@ -572,12 +572,7 @@ class BlinkTraceEvent {
/// Returns null if the value is null.
int? _readInt(Map<String, dynamic> json, String key) {
final num? jsonValue = json[key] as num?;
if (jsonValue == null) {
return null;
}
return jsonValue.toInt();
return jsonValue?.toInt();
}
/// Used by [Chrome.launch] to detect a glibc bug and retry launching the

View File

@@ -1290,11 +1290,7 @@ class PerfTest {
}) {
return inDirectory<TaskResult>(testDirectory, () async {
late Device selectedDevice;
if (device != null) {
selectedDevice = device!;
} else {
selectedDevice = await devices.workingDevice;
}
selectedDevice = device ?? await devices.workingDevice;
await selectedDevice.unlock();
final String deviceId = selectedDevice.deviceId;
final String? localEngine = localEngineFromEnv;

View File

@@ -223,10 +223,7 @@ class _AndroidSemanticsMatcher extends Matcher {
Description describeMismatch(dynamic item, Description mismatchDescription,
Map<dynamic, dynamic> matchState, bool verbose) {
final String? failure = matchState['failure'] as String?;
if (failure == null) {
return mismatchDescription.add('hasAndroidSemantics matcher does not complete successfully');
}
return mismatchDescription.add(failure);
return mismatchDescription.add(failure ?? 'hasAndroidSemantics matcher does not complete successfully');
}
bool _failWithMessage(String value, Map<dynamic, dynamic> matchState) {

View File

@@ -62,12 +62,8 @@ class AppStateModel extends Model {
// Adds a product to the cart.
void addProductToCart(int productId) {
final int? value = _productsInCart[productId];
if (value == null) {
_productsInCart[productId] = 1;
} else {
_productsInCart[productId] = value+1;
}
final int value = _productsInCart[productId] ?? 0;
_productsInCart[productId] = value + 1;
notifyListeners();
}

View File

@@ -86,9 +86,7 @@ Future<void> main(List<String> args) async {
final Map<String, dynamic> tokenFileTokens = _readTokenFile(tokenFile as File);
final String version = tokenFileTokens['version'] as String;
tokenFileTokens.remove('version');
if (versionMap[version] == null) {
versionMap[version] = List<String>.empty(growable: true);
}
versionMap[version] ??= <String>[];
versionMap[version]!.add(tokenFile.uri.pathSegments.last);
tokens.addAll(tokenFileTokens);