Skip to content

Commit fa9992e

Browse files
switch statement cleanup (#148382)
This PR is step 12 in the journey to solve issue #136139 and make the entire Flutter repo more readable. Most of it involves implementing switch expressions, and there's also a few other random things that I wanted to clean up a bit.
1 parent c4ad1dc commit fa9992e

File tree

39 files changed

+301
-529
lines changed

39 files changed

+301
-529
lines changed

dev/bots/analyze_snippet_code.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,10 +1187,8 @@ Future<void> _runInteractive({
11871187
case 'q':
11881188
checker.cleanupTempDirectory();
11891189
exit(0);
1190-
case 'r':
1191-
if (!busy) {
1192-
rerun();
1193-
}
1190+
case 'r' when !busy:
1191+
rerun();
11941192
}
11951193
});
11961194
Watcher(file.absolute.path).events.listen((_) => rerun());

dev/conductor/core/lib/src/start.dart

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -385,23 +385,12 @@ class StartContext extends Context {
385385

386386
/// Determine this release's version number from the [lastVersion] and the [incrementLetter].
387387
Version calculateNextVersion(Version lastVersion, ReleaseType releaseType) {
388-
late final Version nextVersion;
389-
switch (releaseType) {
390-
case ReleaseType.STABLE_INITIAL:
391-
nextVersion = Version(
392-
x: lastVersion.x,
393-
y: lastVersion.y,
394-
z: 0,
395-
type: VersionType.stable,
396-
);
397-
case ReleaseType.STABLE_HOTFIX:
398-
nextVersion = Version.increment(lastVersion, 'z');
399-
case ReleaseType.BETA_INITIAL:
400-
nextVersion = Version.fromCandidateBranch(candidateBranch);
401-
case ReleaseType.BETA_HOTFIX:
402-
nextVersion = Version.increment(lastVersion, 'n');
403-
}
404-
return nextVersion;
388+
return switch (releaseType) {
389+
ReleaseType.STABLE_INITIAL => Version(x: lastVersion.x, y: lastVersion.y, z: 0, type: VersionType.stable),
390+
ReleaseType.STABLE_HOTFIX => Version.increment(lastVersion, 'z'),
391+
ReleaseType.BETA_INITIAL => Version.fromCandidateBranch(candidateBranch),
392+
ReleaseType.BETA_HOTFIX || _ => Version.increment(lastVersion, 'n'),
393+
};
405394
}
406395

407396
/// Ensures the branch point [candidateBranch] and `master` has a version tag.

dev/devicelab/lib/framework/ab.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ class ABTest {
115115
if (value == null) {
116116
value = ''.padRight(len);
117117
} else {
118-
switch (aligns[column]) {
119-
case FieldJustification.LEFT:
120-
value = value.padRight(len);
121-
case FieldJustification.RIGHT:
122-
value = value.padLeft(len);
123-
case FieldJustification.CENTER:
124-
value = value.padLeft((len + value.length) ~/2);
125-
value = value.padRight(len);
126-
}
118+
value = switch (aligns[column]) {
119+
FieldJustification.LEFT => value.padRight(len),
120+
FieldJustification.RIGHT => value.padLeft(len),
121+
FieldJustification.CENTER => value.padLeft((len + value.length) ~/ 2).padRight(len),
122+
};
127123
}
128124
if (column > 0) {
129125
value = value.padLeft(len+1);

dev/devicelab/lib/framework/devices.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,11 @@ class AndroidDeviceDiscovery implements DeviceDiscovery {
252252
}
253253

254254
Future<bool> _matchesCPURequirement(AndroidDevice device) async {
255-
switch (cpu) {
256-
case null:
257-
return true;
258-
case AndroidCPU.arm64:
259-
return device.isArm64();
260-
case AndroidCPU.arm:
261-
return device.isArm();
262-
}
255+
return switch (cpu) {
256+
null => Future<bool>.value(true),
257+
AndroidCPU.arm64 => device.isArm64(),
258+
AndroidCPU.arm => device.isArm(),
259+
};
263260
}
264261

265262
/// Picks a random Android device out of connected devices and sets it as

dev/integration_tests/channels/lib/src/test_step.dart

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,12 @@ class TestStepResult {
3737
});
3838

3939
factory TestStepResult.fromSnapshot(AsyncSnapshot<TestStepResult> snapshot) {
40-
switch (snapshot.connectionState) {
41-
case ConnectionState.none:
42-
return const TestStepResult('Not started', nothing, TestStatus.ok);
43-
case ConnectionState.waiting:
44-
return const TestStepResult('Executing', nothing, TestStatus.pending);
45-
case ConnectionState.done:
46-
if (snapshot.hasData) {
47-
return snapshot.data!;
48-
}
49-
return snapshot.error! as TestStepResult;
50-
case ConnectionState.active:
51-
throw 'Unsupported state ${snapshot.connectionState}';
52-
}
40+
return switch (snapshot.connectionState) {
41+
ConnectionState.none => const TestStepResult('Not started', nothing, TestStatus.ok),
42+
ConnectionState.waiting => const TestStepResult('Executing', nothing, TestStatus.pending),
43+
ConnectionState.done => snapshot.data ?? snapshot.error! as TestStepResult,
44+
ConnectionState.active => throw 'Unsupported state: ConnectionState.active',
45+
};
5346
}
5447

5548
final String name;

dev/integration_tests/flutter_gallery/lib/demo/material/list_demo.dart

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,12 @@ class _ListDemoState extends State<ListDemo> {
204204
@override
205205
Widget build(BuildContext context) {
206206
final String layoutText = _dense != null ? ' \u2013 Dense' : '';
207-
String? itemTypeText;
208-
switch (_itemType) {
209-
case _MaterialListType.oneLine:
210-
case _MaterialListType.oneLineWithAvatar:
211-
itemTypeText = 'Single-line';
212-
case _MaterialListType.twoLine:
213-
itemTypeText = 'Two-line';
214-
case _MaterialListType.threeLine:
215-
itemTypeText = 'Three-line';
216-
case null:
217-
break;
218-
}
207+
final String? itemTypeText = switch (_itemType) {
208+
_MaterialListType.oneLine || _MaterialListType.oneLineWithAvatar => 'Single-line',
209+
_MaterialListType.twoLine => 'Two-line',
210+
_MaterialListType.threeLine => 'Three-line',
211+
null => null,
212+
};
219213

220214
Iterable<Widget> listTiles = items.map<Widget>((String item) => buildListTile(context, item));
221215
if (_showDividers != null) {

dev/integration_tests/flutter_gallery/lib/demo/material/slider_demo.dart

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,12 @@ class _CustomRangeThumbShape extends RangeSliderThumbShape {
8080
);
8181

8282
final double size = _thumbSize * sizeTween.evaluate(enableAnimation);
83-
late Path thumbPath;
84-
switch (textDirection) {
85-
case TextDirection.rtl:
86-
switch (thumb) {
87-
case Thumb.start:
88-
thumbPath = _rightTriangle(size, center);
89-
case Thumb.end:
90-
thumbPath = _leftTriangle(size, center);
91-
case null:
92-
break;
93-
}
94-
case TextDirection.ltr:
95-
switch (thumb) {
96-
case Thumb.start:
97-
thumbPath = _leftTriangle(size, center);
98-
case Thumb.end:
99-
thumbPath = _rightTriangle(size, center);
100-
case null:
101-
break;
102-
}
103-
case null:
104-
break;
105-
}
83+
final Path thumbPath = switch ((textDirection!, thumb!)) {
84+
(TextDirection.rtl, Thumb.start) => _rightTriangle(size, center),
85+
(TextDirection.rtl, Thumb.end) => _leftTriangle(size, center),
86+
(TextDirection.ltr, Thumb.start) => _leftTriangle(size, center),
87+
(TextDirection.ltr, Thumb.end) => _rightTriangle(size, center),
88+
};
10689
canvas.drawPath(thumbPath, Paint()..color = colorTween.evaluate(enableAnimation)!);
10790
}
10891
}

dev/integration_tests/new_gallery/lib/data/demos.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ enum GalleryDemoCategory {
5050
}
5151

5252
String? displayTitle(GalleryLocalizations localizations) {
53-
switch (this) {
54-
case GalleryDemoCategory.other:
55-
return localizations.homeCategoryReference;
56-
case GalleryDemoCategory.material:
57-
case GalleryDemoCategory.cupertino:
58-
return toString();
59-
case GalleryDemoCategory.study:
60-
}
61-
return null;
53+
return switch (this) {
54+
study => null,
55+
material || cupertino => toString(),
56+
other => localizations.homeCategoryReference,
57+
};
6258
}
6359
}
6460

dev/integration_tests/new_gallery/lib/data/gallery_options.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,15 @@ class GalleryOptions {
9696
/// In other words, if the theme is dark, returns light; if the theme is
9797
/// light, returns dark.
9898
SystemUiOverlayStyle resolvedSystemUiOverlayStyle() {
99-
Brightness brightness;
100-
switch (themeMode) {
101-
case ThemeMode.light:
102-
brightness = Brightness.light;
103-
case ThemeMode.dark:
104-
brightness = Brightness.dark;
105-
case ThemeMode.system:
106-
brightness =
107-
WidgetsBinding.instance.platformDispatcher.platformBrightness;
108-
}
109-
110-
final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
111-
? SystemUiOverlayStyle.light
112-
: SystemUiOverlayStyle.dark;
113-
114-
return overlayStyle;
99+
final Brightness brightness = switch (themeMode) {
100+
ThemeMode.light => Brightness.light,
101+
ThemeMode.dark => Brightness.dark,
102+
ThemeMode.system => WidgetsBinding.instance.platformDispatcher.platformBrightness,
103+
};
104+
return switch (brightness) {
105+
Brightness.light => SystemUiOverlayStyle.dark,
106+
Brightness.dark => SystemUiOverlayStyle.light,
107+
};
115108
}
116109

117110
GalleryOptions copyWith({

dev/integration_tests/new_gallery/lib/demos/cupertino/cupertino_alert_demo.dart

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,13 @@ class _CupertinoAlertDemoState extends State<CupertinoAlertDemo>
105105

106106
String _title(BuildContext context) {
107107
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
108-
switch (widget.type) {
109-
case AlertDemoType.alert:
110-
return localizations.demoCupertinoAlertTitle;
111-
case AlertDemoType.alertTitle:
112-
return localizations.demoCupertinoAlertWithTitleTitle;
113-
case AlertDemoType.alertButtons:
114-
return localizations.demoCupertinoAlertButtonsTitle;
115-
case AlertDemoType.alertButtonsOnly:
116-
return localizations.demoCupertinoAlertButtonsOnlyTitle;
117-
case AlertDemoType.actionSheet:
118-
return localizations.demoCupertinoActionSheetTitle;
119-
}
108+
return switch (widget.type) {
109+
AlertDemoType.alert => localizations.demoCupertinoAlertTitle,
110+
AlertDemoType.alertTitle => localizations.demoCupertinoAlertWithTitleTitle,
111+
AlertDemoType.alertButtons => localizations.demoCupertinoAlertButtonsTitle,
112+
AlertDemoType.alertButtonsOnly => localizations.demoCupertinoAlertButtonsOnlyTitle,
113+
AlertDemoType.actionSheet => localizations.demoCupertinoActionSheetTitle,
114+
};
120115
}
121116

122117
static Route<String> _alertDemoDialog(

0 commit comments

Comments
 (0)