Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 38630b6

Browse files
authored
Remove unnecessary null checks in flutter_tool (#118857)
* dart fix --apply * manual fixes * fix after merge conflicts * review
1 parent edb571e commit 38630b6

File tree

108 files changed

+287
-654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+287
-654
lines changed

packages/flutter_tools/lib/src/android/android_device_discovery.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class AndroidDevices extends PollingDeviceDiscovery {
147147

148148
final String deviceID = match[1]!;
149149
final String deviceState = match[2]!;
150-
String rest = match[3]!;
150+
String? rest = match[3];
151151

152152
final Map<String, String> info = <String, String>{};
153153
if (rest != null && rest.isNotEmpty) {

packages/flutter_tools/lib/src/android/android_emulator.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ class AndroidEmulators extends EmulatorDiscovery {
6262
<String>[emulatorPath, '-list-avds'])).stdout.trim();
6363

6464
final List<AndroidEmulator> emulators = <AndroidEmulator>[];
65-
if (listAvdsOutput != null) {
66-
_extractEmulatorAvdInfo(listAvdsOutput, emulators);
67-
}
65+
_extractEmulatorAvdInfo(listAvdsOutput, emulators);
6866
return emulators;
6967
}
7068

packages/flutter_tools/lib/src/android/android_sdk.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,9 @@ class AndroidSdk {
435435
throwOnError: true,
436436
hideStdout: true,
437437
).stdout.trim();
438-
if (javaHomeOutput != null) {
439-
if ((javaHomeOutput != null) && (javaHomeOutput.isNotEmpty)) {
440-
final String javaHome = javaHomeOutput.split('\n').last.trim();
441-
return fileSystem.path.join(javaHome, 'bin', 'java');
442-
}
438+
if (javaHomeOutput.isNotEmpty) {
439+
final String javaHome = javaHomeOutput.split('\n').last.trim();
440+
return fileSystem.path.join(javaHome, 'bin', 'java');
443441
}
444442
} on Exception { /* ignore */ }
445443
}
@@ -500,10 +498,7 @@ class AndroidSdkVersion implements Comparable<AndroidSdkVersion> {
500498
required this.platformName,
501499
required this.buildToolsVersion,
502500
required FileSystem fileSystem,
503-
}) : assert(sdkLevel != null),
504-
assert(platformName != null),
505-
assert(buildToolsVersion != null),
506-
_fileSystem = fileSystem;
501+
}) : _fileSystem = fileSystem;
507502

508503
final AndroidSdk sdk;
509504
final int sdkLevel;

packages/flutter_tools/lib/src/android/android_studio.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class AndroidStudio implements Comparable<AndroidStudio> {
110110
// and <base dir>/system/.home for Android Studio < 4.1
111111
String dotHomeFilePath;
112112

113-
if (major != null && major >= 4 && minor != null && minor >= 1) {
113+
if (major >= 4 && minor >= 1) {
114114
dotHomeFilePath = globals.fs.path.join(homeDotDir.path, '.home');
115115
} else {
116116
dotHomeFilePath =
@@ -161,7 +161,7 @@ class AndroidStudio implements Comparable<AndroidStudio> {
161161
}
162162
if (globals.platform.isMacOS) {
163163
/// plugin path of Android Studio has been changed after version 4.1.
164-
if (major != null && major >= 4 && minor != null && minor >= 1) {
164+
if (major >= 4 && minor >= 1) {
165165
return globals.fs.path.join(
166166
homeDirPath,
167167
'Library',
@@ -185,7 +185,7 @@ class AndroidStudio implements Comparable<AndroidStudio> {
185185
return toolboxPluginsPath;
186186
}
187187

188-
if (major != null && major >= 4 && minor != null && minor >= 1 &&
188+
if (major >= 4 && minor >= 1 &&
189189
globals.platform.isLinux) {
190190
return globals.fs.path.join(
191191
homeDirPath,
@@ -394,7 +394,7 @@ class AndroidStudio implements Comparable<AndroidStudio> {
394394
version: Version.parse(version),
395395
studioAppName: title,
396396
);
397-
if (studio != null && !hasStudioAt(studio.directory, newerThan: studio.version)) {
397+
if (!hasStudioAt(studio.directory, newerThan: studio.version)) {
398398
studios.removeWhere((AndroidStudio other) => other.directory == studio.directory);
399399
studios.add(studio);
400400
}
@@ -425,9 +425,6 @@ class AndroidStudio implements Comparable<AndroidStudio> {
425425
}
426426

427427
static String? extractStudioPlistValueWithMatcher(String plistValue, RegExp keyMatcher) {
428-
if (plistValue == null || keyMatcher == null) {
429-
return null;
430-
}
431428
return keyMatcher.stringMatch(plistValue)?.split('=').last.trim().replaceAll('"', '');
432429
}
433430

packages/flutter_tools/lib/src/android/application_package.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ class AndroidApk extends ApplicationPackage implements PrebuiltApplicationPackag
2727
required this.applicationPackage,
2828
required this.versionCode,
2929
required this.launchActivity,
30-
}) : assert(applicationPackage != null),
31-
assert(launchActivity != null);
30+
});
3231

3332
/// Creates a new AndroidApk from an existing APK.
3433
///
@@ -294,7 +293,7 @@ class ApkManifestData {
294293
}
295294

296295
static ApkManifestData? parseFromXmlDump(String data, Logger logger) {
297-
if (data == null || data.trim().isEmpty) {
296+
if (data.trim().isEmpty) {
298297
return null;
299298
}
300299

packages/flutter_tools/lib/src/android/deferred_components_gen_snapshot_validator.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,6 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
187187
loadingUnitComparisonResults = <String, Object>{};
188188
final Set<LoadingUnit> unmatchedLoadingUnits = <LoadingUnit>{};
189189
final List<LoadingUnit> newLoadingUnits = <LoadingUnit>[];
190-
if (generatedLoadingUnits == null || cachedLoadingUnits == null) {
191-
loadingUnitComparisonResults!['new'] = newLoadingUnits;
192-
loadingUnitComparisonResults!['missing'] = unmatchedLoadingUnits;
193-
loadingUnitComparisonResults!['match'] = false;
194-
return false;
195-
}
196190
unmatchedLoadingUnits.addAll(cachedLoadingUnits);
197191
final Set<int> addedNewIds = <int>{};
198192
for (final LoadingUnit genUnit in generatedLoadingUnits) {
@@ -335,7 +329,7 @@ loading-units:
335329
continue;
336330
}
337331
buffer.write(' - id: ${unit.id}\n');
338-
if (unit.libraries != null && unit.libraries.isNotEmpty) {
332+
if (unit.libraries.isNotEmpty) {
339333
buffer.write(' libraries:\n');
340334
for (final String lib in unit.libraries) {
341335
buffer.write(' - $lib\n');

packages/flutter_tools/lib/src/android/deferred_components_prebuild_validator.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DeferredComponentsPrebuildValidator extends DeferredComponentsValidator {
5151
/// valid, as there are many ways that they can be validly configured.
5252
Future<bool> checkAndroidDynamicFeature(List<DeferredComponent> components) async {
5353
inputs.add(projectDir.childFile('pubspec.yaml'));
54-
if (components == null || components.isEmpty) {
54+
if (components.isEmpty) {
5555
return false;
5656
}
5757
bool changesMade = false;
@@ -124,7 +124,7 @@ class DeferredComponentsPrebuildValidator extends DeferredComponentsValidator {
124124
.childDirectory('values')
125125
.childFile('strings.xml');
126126
ErrorHandlingFileSystem.deleteIfExists(stringResOutput);
127-
if (components == null || components.isEmpty) {
127+
if (components.isEmpty) {
128128
return true;
129129
}
130130
final Map<String, String> requiredEntriesMap = <String, String>{};
@@ -148,7 +148,7 @@ class DeferredComponentsPrebuildValidator extends DeferredComponentsValidator {
148148
for (final XmlElement element in resources.findElements('string')) {
149149
final String? name = element.getAttribute('name');
150150
if (requiredEntriesMap.containsKey(name)) {
151-
if (element.text != null && element.text != requiredEntriesMap[name]) {
151+
if (element.text != requiredEntriesMap[name]) {
152152
element.innerText = requiredEntriesMap[name]!;
153153
modified = true;
154154
}

packages/flutter_tools/lib/src/android/deferred_components_validator.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ abstract class DeferredComponentsValidator {
122122
}
123123
}
124124
// Log diff file contents, with color highlighting
125-
if (diffLines != null && diffLines.isNotEmpty) {
125+
if (diffLines.isNotEmpty) {
126126
logger.printStatus('Diff between `android` and expected files:', emphasis: true);
127127
logger.printStatus('');
128128
for (final String line in diffLines) {
@@ -163,7 +163,7 @@ abstract class DeferredComponentsValidator {
163163
The above files have been placed into `build/$kDeferredComponentsTempDirectory`,
164164
a temporary directory. The files should be reviewed and moved into the project's
165165
`android` directory.''');
166-
if (diffLines != null && diffLines.isNotEmpty && !platform.isWindows) {
166+
if (diffLines.isNotEmpty && !platform.isWindows) {
167167
logger.printStatus(r'''
168168
169169
The recommended changes can be quickly applied by running:

packages/flutter_tools/lib/src/android/gradle.dart

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,6 @@ class AndroidGradleBuilder implements AndroidBuilder {
229229
int retry = 0,
230230
@visibleForTesting int? maxRetries,
231231
}) async {
232-
assert(project != null);
233-
assert(androidBuildInfo != null);
234-
assert(target != null);
235-
assert(isBuildingBundle != null);
236-
assert(localGradleErrors != null);
237232

238233
if (!project.android.isSupportedVersion) {
239234
_exitWithUnsupportedProjectMessage(_usage, _logger.terminal);
@@ -299,9 +294,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
299294
.map(getPlatformNameForAndroidArch).join(',');
300295
command.add('-Ptarget-platform=$targetPlatforms');
301296
}
302-
if (target != null) {
303-
command.add('-Ptarget=$target');
304-
}
297+
command.add('-Ptarget=$target');
305298
// Only attempt adding multidex support if all the flutter generated files exist.
306299
// If the files do not exist and it was unintentional, the app will fail to build
307300
// and prompt the developer if they wish Flutter to add the files again via gradle_error.dart.
@@ -342,7 +335,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
342335
}
343336
}
344337
command.addAll(androidBuildInfo.buildInfo.toGradleConfig());
345-
if (buildInfo.fileSystemRoots != null && buildInfo.fileSystemRoots.isNotEmpty) {
338+
if (buildInfo.fileSystemRoots.isNotEmpty) {
346339
command.add('-Pfilesystem-roots=${buildInfo.fileSystemRoots.join('|')}');
347340
}
348341
if (buildInfo.fileSystemScheme != null) {
@@ -557,10 +550,6 @@ class AndroidGradleBuilder implements AndroidBuilder {
557550
required Directory outputDirectory,
558551
required String buildNumber,
559552
}) async {
560-
assert(project != null);
561-
assert(target != null);
562-
assert(androidBuildInfo != null);
563-
assert(outputDirectory != null);
564553

565554
final FlutterManifest manifest = project.manifest;
566555
if (!manifest.isModule && !manifest.isPlugin) {
@@ -600,7 +589,7 @@ class AndroidGradleBuilder implements AndroidBuilder {
600589
command.add('--no-daemon');
601590
}
602591

603-
if (target != null && target.isNotEmpty) {
592+
if (target.isNotEmpty) {
604593
command.add('-Ptarget=$target');
605594
}
606595
command.addAll(androidBuildInfo.buildInfo.toGradleConfig());
@@ -699,8 +688,7 @@ void printHowToConsumeAar({
699688
required FileSystem fileSystem,
700689
String? buildNumber,
701690
}) {
702-
assert(buildModes != null && buildModes.isNotEmpty);
703-
assert(repoDirectory != null);
691+
assert(buildModes.isNotEmpty);
704692
buildNumber ??= '1.0';
705693

706694
logger.printStatus('\nConsuming the Module', emphasis: true);
@@ -913,8 +901,6 @@ Never _exitWithExpectedFileNotFound({
913901
required Logger logger,
914902
required Usage usage,
915903
}) {
916-
assert(project != null);
917-
assert(fileExtension != null);
918904

919905
final String androidGradlePluginVersion =
920906
getGradleVersionForAndroidPlugin(project.android.hostAppGradleRoot, logger);
@@ -984,8 +970,6 @@ Directory _getLocalEngineRepo({
984970
required AndroidBuildInfo androidBuildInfo,
985971
required FileSystem fileSystem,
986972
}) {
987-
assert(engineOutPath != null);
988-
assert(androidBuildInfo != null);
989973

990974
final String abi = _getAbiByLocalEnginePath(engineOutPath);
991975
final Directory localEngineRepo = fileSystem.systemTempDirectory

packages/flutter_tools/lib/src/android/gradle_errors.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ final GradleHandledError licenseNotAcceptedHandler = GradleHandledError(
337337
r'You have not accepted the license agreements of the following SDK components:\s*\[(.+)\]';
338338

339339
final RegExp licenseFailure = RegExp(licenseNotAcceptedMatcher, multiLine: true);
340-
assert(licenseFailure != null);
341340
final Match? licenseMatch = licenseFailure.firstMatch(line);
342341
globals.printBox(
343342
'${globals.logger.terminal.warningMark} Unable to download needed Android SDK components, as the '

0 commit comments

Comments
 (0)