From fa9992eff6ac908d2ea04861cf64e83ed53ce02e Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 16 May 2024 18:16:06 -0600 Subject: [PATCH] `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. --- dev/bots/analyze_snippet_code.dart | 6 +- dev/conductor/core/lib/src/start.dart | 23 +--- dev/devicelab/lib/framework/ab.dart | 14 +- dev/devicelab/lib/framework/devices.dart | 13 +- .../channels/lib/src/test_step.dart | 19 +-- .../lib/demo/material/list_demo.dart | 18 +-- .../lib/demo/material/slider_demo.dart | 29 +--- .../new_gallery/lib/data/demos.dart | 14 +- .../new_gallery/lib/data/gallery_options.dart | 25 ++-- .../demos/cupertino/cupertino_alert_demo.dart | 19 +-- .../lib/demos/material/bottom_sheet_demo.dart | 20 ++- .../lib/demos/material/button_demo.dart | 19 +-- .../lib/demos/material/chip_demo.dart | 16 +-- .../lib/demos/material/dialog_demo.dart | 16 +-- .../lib/demos/material/picker_demo.dart | 13 +- .../lib/demos/material/tabs_demo.dart | 12 +- .../new_gallery/lib/pages/demo.dart | 15 +- .../new_gallery/lib/studies/reply/app.dart | 18 +-- .../lib/studies/reply/mailbox_body.dart | 41 ++---- .../lib/src/test_step.dart | 21 +-- dev/snippets/lib/src/data_types.dart | 30 ++-- .../localization/localizations_utils.dart | 29 +--- .../lib/src/cupertino/text_selection.dart | 5 +- .../text_selection_toolbar_button.dart | 6 +- .../lib/src/material/navigation_bar.dart | 4 +- .../flutter/lib/src/widgets/drag_target.dart | 13 +- .../lib/src/widgets/scroll_physics.dart | 10 +- .../flutter/lib/src/widgets/scrollbar.dart | 8 +- .../src/common/deserialization_factory.dart | 77 +++++------ .../flutter_driver/lib/src/common/find.dart | 14 +- .../lib/src/common/handler_factory.dart | 128 ++++++++---------- .../flutter_driver/lib/src/common/wait.dart | 25 ++-- .../lib/src/driver/profiling_summarizer.dart | 16 +-- .../lib/src/driver/vmservice_driver.dart | 24 ++-- .../lib/src/extension/wait_conditions.dart | 22 ++- .../lib/src/material_localizations.dart | 10 +- .../src/test_default_binary_messenger.dart | 18 ++- .../lib/src/flutter_project_metadata.dart | 4 +- packages/integration_test/lib/common.dart | 16 +-- 39 files changed, 301 insertions(+), 529 deletions(-) diff --git a/dev/bots/analyze_snippet_code.dart b/dev/bots/analyze_snippet_code.dart index 924ae2a03e..43bf55b6fc 100644 --- a/dev/bots/analyze_snippet_code.dart +++ b/dev/bots/analyze_snippet_code.dart @@ -1187,10 +1187,8 @@ Future _runInteractive({ case 'q': checker.cleanupTempDirectory(); exit(0); - case 'r': - if (!busy) { - rerun(); - } + case 'r' when !busy: + rerun(); } }); Watcher(file.absolute.path).events.listen((_) => rerun()); diff --git a/dev/conductor/core/lib/src/start.dart b/dev/conductor/core/lib/src/start.dart index 8dfc0463c6..01446d846e 100644 --- a/dev/conductor/core/lib/src/start.dart +++ b/dev/conductor/core/lib/src/start.dart @@ -385,23 +385,12 @@ class StartContext extends Context { /// Determine this release's version number from the [lastVersion] and the [incrementLetter]. Version calculateNextVersion(Version lastVersion, ReleaseType releaseType) { - late final Version nextVersion; - switch (releaseType) { - case ReleaseType.STABLE_INITIAL: - nextVersion = Version( - x: lastVersion.x, - y: lastVersion.y, - z: 0, - type: VersionType.stable, - ); - case ReleaseType.STABLE_HOTFIX: - nextVersion = Version.increment(lastVersion, 'z'); - case ReleaseType.BETA_INITIAL: - nextVersion = Version.fromCandidateBranch(candidateBranch); - case ReleaseType.BETA_HOTFIX: - nextVersion = Version.increment(lastVersion, 'n'); - } - return nextVersion; + return switch (releaseType) { + ReleaseType.STABLE_INITIAL => Version(x: lastVersion.x, y: lastVersion.y, z: 0, type: VersionType.stable), + ReleaseType.STABLE_HOTFIX => Version.increment(lastVersion, 'z'), + ReleaseType.BETA_INITIAL => Version.fromCandidateBranch(candidateBranch), + ReleaseType.BETA_HOTFIX || _ => Version.increment(lastVersion, 'n'), + }; } /// Ensures the branch point [candidateBranch] and `master` has a version tag. diff --git a/dev/devicelab/lib/framework/ab.dart b/dev/devicelab/lib/framework/ab.dart index 0e05a130bf..160d1ec610 100644 --- a/dev/devicelab/lib/framework/ab.dart +++ b/dev/devicelab/lib/framework/ab.dart @@ -115,15 +115,11 @@ class ABTest { if (value == null) { value = ''.padRight(len); } else { - switch (aligns[column]) { - case FieldJustification.LEFT: - value = value.padRight(len); - case FieldJustification.RIGHT: - value = value.padLeft(len); - case FieldJustification.CENTER: - value = value.padLeft((len + value.length) ~/2); - value = value.padRight(len); - } + value = switch (aligns[column]) { + FieldJustification.LEFT => value.padRight(len), + FieldJustification.RIGHT => value.padLeft(len), + FieldJustification.CENTER => value.padLeft((len + value.length) ~/ 2).padRight(len), + }; } if (column > 0) { value = value.padLeft(len+1); diff --git a/dev/devicelab/lib/framework/devices.dart b/dev/devicelab/lib/framework/devices.dart index c73247620c..03ec9efb90 100644 --- a/dev/devicelab/lib/framework/devices.dart +++ b/dev/devicelab/lib/framework/devices.dart @@ -252,14 +252,11 @@ class AndroidDeviceDiscovery implements DeviceDiscovery { } Future _matchesCPURequirement(AndroidDevice device) async { - switch (cpu) { - case null: - return true; - case AndroidCPU.arm64: - return device.isArm64(); - case AndroidCPU.arm: - return device.isArm(); - } + return switch (cpu) { + null => Future.value(true), + AndroidCPU.arm64 => device.isArm64(), + AndroidCPU.arm => device.isArm(), + }; } /// Picks a random Android device out of connected devices and sets it as diff --git a/dev/integration_tests/channels/lib/src/test_step.dart b/dev/integration_tests/channels/lib/src/test_step.dart index 443db5a6fa..44ae8a35db 100644 --- a/dev/integration_tests/channels/lib/src/test_step.dart +++ b/dev/integration_tests/channels/lib/src/test_step.dart @@ -37,19 +37,12 @@ class TestStepResult { }); factory TestStepResult.fromSnapshot(AsyncSnapshot snapshot) { - switch (snapshot.connectionState) { - case ConnectionState.none: - return const TestStepResult('Not started', nothing, TestStatus.ok); - case ConnectionState.waiting: - return const TestStepResult('Executing', nothing, TestStatus.pending); - case ConnectionState.done: - if (snapshot.hasData) { - return snapshot.data!; - } - return snapshot.error! as TestStepResult; - case ConnectionState.active: - throw 'Unsupported state ${snapshot.connectionState}'; - } + return switch (snapshot.connectionState) { + ConnectionState.none => const TestStepResult('Not started', nothing, TestStatus.ok), + ConnectionState.waiting => const TestStepResult('Executing', nothing, TestStatus.pending), + ConnectionState.done => snapshot.data ?? snapshot.error! as TestStepResult, + ConnectionState.active => throw 'Unsupported state: ConnectionState.active', + }; } final String name; diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/list_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/list_demo.dart index 7f6551f865..2c46f28710 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/list_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/list_demo.dart @@ -204,18 +204,12 @@ class _ListDemoState extends State { @override Widget build(BuildContext context) { final String layoutText = _dense != null ? ' \u2013 Dense' : ''; - String? itemTypeText; - switch (_itemType) { - case _MaterialListType.oneLine: - case _MaterialListType.oneLineWithAvatar: - itemTypeText = 'Single-line'; - case _MaterialListType.twoLine: - itemTypeText = 'Two-line'; - case _MaterialListType.threeLine: - itemTypeText = 'Three-line'; - case null: - break; - } + final String? itemTypeText = switch (_itemType) { + _MaterialListType.oneLine || _MaterialListType.oneLineWithAvatar => 'Single-line', + _MaterialListType.twoLine => 'Two-line', + _MaterialListType.threeLine => 'Three-line', + null => null, + }; Iterable listTiles = items.map((String item) => buildListTile(context, item)); if (_showDividers != null) { diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/slider_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/slider_demo.dart index 1465d6baeb..d3da12cee1 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/slider_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/slider_demo.dart @@ -80,29 +80,12 @@ class _CustomRangeThumbShape extends RangeSliderThumbShape { ); final double size = _thumbSize * sizeTween.evaluate(enableAnimation); - late Path thumbPath; - switch (textDirection) { - case TextDirection.rtl: - switch (thumb) { - case Thumb.start: - thumbPath = _rightTriangle(size, center); - case Thumb.end: - thumbPath = _leftTriangle(size, center); - case null: - break; - } - case TextDirection.ltr: - switch (thumb) { - case Thumb.start: - thumbPath = _leftTriangle(size, center); - case Thumb.end: - thumbPath = _rightTriangle(size, center); - case null: - break; - } - case null: - break; - } + final Path thumbPath = switch ((textDirection!, thumb!)) { + (TextDirection.rtl, Thumb.start) => _rightTriangle(size, center), + (TextDirection.rtl, Thumb.end) => _leftTriangle(size, center), + (TextDirection.ltr, Thumb.start) => _leftTriangle(size, center), + (TextDirection.ltr, Thumb.end) => _rightTriangle(size, center), + }; canvas.drawPath(thumbPath, Paint()..color = colorTween.evaluate(enableAnimation)!); } } diff --git a/dev/integration_tests/new_gallery/lib/data/demos.dart b/dev/integration_tests/new_gallery/lib/data/demos.dart index 48d8049ae2..3f63904662 100644 --- a/dev/integration_tests/new_gallery/lib/data/demos.dart +++ b/dev/integration_tests/new_gallery/lib/data/demos.dart @@ -50,15 +50,11 @@ enum GalleryDemoCategory { } String? displayTitle(GalleryLocalizations localizations) { - switch (this) { - case GalleryDemoCategory.other: - return localizations.homeCategoryReference; - case GalleryDemoCategory.material: - case GalleryDemoCategory.cupertino: - return toString(); - case GalleryDemoCategory.study: - } - return null; + return switch (this) { + study => null, + material || cupertino => toString(), + other => localizations.homeCategoryReference, + }; } } diff --git a/dev/integration_tests/new_gallery/lib/data/gallery_options.dart b/dev/integration_tests/new_gallery/lib/data/gallery_options.dart index b896e4dcc0..46ddf22af8 100644 --- a/dev/integration_tests/new_gallery/lib/data/gallery_options.dart +++ b/dev/integration_tests/new_gallery/lib/data/gallery_options.dart @@ -96,22 +96,15 @@ class GalleryOptions { /// In other words, if the theme is dark, returns light; if the theme is /// light, returns dark. SystemUiOverlayStyle resolvedSystemUiOverlayStyle() { - Brightness brightness; - switch (themeMode) { - case ThemeMode.light: - brightness = Brightness.light; - case ThemeMode.dark: - brightness = Brightness.dark; - case ThemeMode.system: - brightness = - WidgetsBinding.instance.platformDispatcher.platformBrightness; - } - - final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark - ? SystemUiOverlayStyle.light - : SystemUiOverlayStyle.dark; - - return overlayStyle; + final Brightness brightness = switch (themeMode) { + ThemeMode.light => Brightness.light, + ThemeMode.dark => Brightness.dark, + ThemeMode.system => WidgetsBinding.instance.platformDispatcher.platformBrightness, + }; + return switch (brightness) { + Brightness.light => SystemUiOverlayStyle.dark, + Brightness.dark => SystemUiOverlayStyle.light, + }; } GalleryOptions copyWith({ diff --git a/dev/integration_tests/new_gallery/lib/demos/cupertino/cupertino_alert_demo.dart b/dev/integration_tests/new_gallery/lib/demos/cupertino/cupertino_alert_demo.dart index 96cc821c2d..bcc856f58c 100644 --- a/dev/integration_tests/new_gallery/lib/demos/cupertino/cupertino_alert_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/cupertino/cupertino_alert_demo.dart @@ -105,18 +105,13 @@ class _CupertinoAlertDemoState extends State String _title(BuildContext context) { final GalleryLocalizations localizations = GalleryLocalizations.of(context)!; - switch (widget.type) { - case AlertDemoType.alert: - return localizations.demoCupertinoAlertTitle; - case AlertDemoType.alertTitle: - return localizations.demoCupertinoAlertWithTitleTitle; - case AlertDemoType.alertButtons: - return localizations.demoCupertinoAlertButtonsTitle; - case AlertDemoType.alertButtonsOnly: - return localizations.demoCupertinoAlertButtonsOnlyTitle; - case AlertDemoType.actionSheet: - return localizations.demoCupertinoActionSheetTitle; - } + return switch (widget.type) { + AlertDemoType.alert => localizations.demoCupertinoAlertTitle, + AlertDemoType.alertTitle => localizations.demoCupertinoAlertWithTitleTitle, + AlertDemoType.alertButtons => localizations.demoCupertinoAlertButtonsTitle, + AlertDemoType.alertButtonsOnly => localizations.demoCupertinoAlertButtonsOnlyTitle, + AlertDemoType.actionSheet => localizations.demoCupertinoActionSheetTitle, + }; } static Route _alertDemoDialog( diff --git a/dev/integration_tests/new_gallery/lib/demos/material/bottom_sheet_demo.dart b/dev/integration_tests/new_gallery/lib/demos/material/bottom_sheet_demo.dart index 5b029822e1..723cd0ee9c 100644 --- a/dev/integration_tests/new_gallery/lib/demos/material/bottom_sheet_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/material/bottom_sheet_demo.dart @@ -17,21 +17,17 @@ class BottomSheetDemo extends StatelessWidget { String _title(BuildContext context) { final GalleryLocalizations localizations = GalleryLocalizations.of(context)!; - switch (type) { - case BottomSheetDemoType.persistent: - return localizations.demoBottomSheetPersistentTitle; - case BottomSheetDemoType.modal: - return localizations.demoBottomSheetModalTitle; - } + return switch (type) { + BottomSheetDemoType.persistent => localizations.demoBottomSheetPersistentTitle, + BottomSheetDemoType.modal => localizations.demoBottomSheetModalTitle, + }; } Widget _bottomSheetDemo(BuildContext context) { - switch (type) { - case BottomSheetDemoType.persistent: - return _PersistentBottomSheetDemo(); - case BottomSheetDemoType.modal: - return _ModalBottomSheetDemo(); - } + return switch (type) { + BottomSheetDemoType.persistent => _PersistentBottomSheetDemo(), + BottomSheetDemoType.modal => _ModalBottomSheetDemo(), + }; } @override diff --git a/dev/integration_tests/new_gallery/lib/demos/material/button_demo.dart b/dev/integration_tests/new_gallery/lib/demos/material/button_demo.dart index 9755b6f816..e28262d1fc 100644 --- a/dev/integration_tests/new_gallery/lib/demos/material/button_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/material/button_demo.dart @@ -13,18 +13,13 @@ class ButtonDemo extends StatelessWidget { String _title(BuildContext context) { final GalleryLocalizations localizations = GalleryLocalizations.of(context)!; - switch (type) { - case ButtonDemoType.text: - return localizations.demoTextButtonTitle; - case ButtonDemoType.elevated: - return localizations.demoElevatedButtonTitle; - case ButtonDemoType.outlined: - return localizations.demoOutlinedButtonTitle; - case ButtonDemoType.toggle: - return localizations.demoToggleButtonTitle; - case ButtonDemoType.floating: - return localizations.demoFloatingButtonTitle; - } + return switch (type) { + ButtonDemoType.text => localizations.demoTextButtonTitle, + ButtonDemoType.elevated => localizations.demoElevatedButtonTitle, + ButtonDemoType.outlined => localizations.demoOutlinedButtonTitle, + ButtonDemoType.toggle => localizations.demoToggleButtonTitle, + ButtonDemoType.floating => localizations.demoFloatingButtonTitle, + }; } @override diff --git a/dev/integration_tests/new_gallery/lib/demos/material/chip_demo.dart b/dev/integration_tests/new_gallery/lib/demos/material/chip_demo.dart index 110b5e143c..48f842e88c 100644 --- a/dev/integration_tests/new_gallery/lib/demos/material/chip_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/material/chip_demo.dart @@ -16,16 +16,12 @@ class ChipDemo extends StatelessWidget { String _title(BuildContext context) { final GalleryLocalizations localizations = GalleryLocalizations.of(context)!; - switch (type) { - case ChipDemoType.action: - return localizations.demoActionChipTitle; - case ChipDemoType.choice: - return localizations.demoChoiceChipTitle; - case ChipDemoType.filter: - return localizations.demoFilterChipTitle; - case ChipDemoType.input: - return localizations.demoInputChipTitle; - } + return switch (type) { + ChipDemoType.action => localizations.demoActionChipTitle, + ChipDemoType.choice => localizations.demoChoiceChipTitle, + ChipDemoType.filter => localizations.demoFilterChipTitle, + ChipDemoType.input => localizations.demoInputChipTitle, + }; } @override diff --git a/dev/integration_tests/new_gallery/lib/demos/material/dialog_demo.dart b/dev/integration_tests/new_gallery/lib/demos/material/dialog_demo.dart index e9cb3c3fc2..19a3cdc17a 100644 --- a/dev/integration_tests/new_gallery/lib/demos/material/dialog_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/material/dialog_demo.dart @@ -81,16 +81,12 @@ class _DialogDemoState extends State with RestorationMixin { String _title(BuildContext context) { final GalleryLocalizations localizations = GalleryLocalizations.of(context)!; - switch (widget.type) { - case DialogDemoType.alert: - return localizations.demoAlertDialogTitle; - case DialogDemoType.alertTitle: - return localizations.demoAlertTitleDialogTitle; - case DialogDemoType.simple: - return localizations.demoSimpleDialogTitle; - case DialogDemoType.fullscreen: - return localizations.demoFullscreenDialogTitle; - } + return switch (widget.type) { + DialogDemoType.alert => localizations.demoAlertDialogTitle, + DialogDemoType.alertTitle => localizations.demoAlertTitleDialogTitle, + DialogDemoType.simple => localizations.demoSimpleDialogTitle, + DialogDemoType.fullscreen => localizations.demoFullscreenDialogTitle, + }; } static Route _alertDialogDemoRoute( diff --git a/dev/integration_tests/new_gallery/lib/demos/material/picker_demo.dart b/dev/integration_tests/new_gallery/lib/demos/material/picker_demo.dart index 98b073b639..26bfba5d3f 100644 --- a/dev/integration_tests/new_gallery/lib/demos/material/picker_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/material/picker_demo.dart @@ -164,14 +164,11 @@ class _PickerDemoState extends State with RestorationMixin { String get _title { final GalleryLocalizations localizations = GalleryLocalizations.of(context)!; - switch (widget.type) { - case PickerDemoType.date: - return localizations.demoDatePickerTitle; - case PickerDemoType.time: - return localizations.demoTimePickerTitle; - case PickerDemoType.range: - return localizations.demoDateRangePickerTitle; - } + return switch (widget.type) { + PickerDemoType.date => localizations.demoDatePickerTitle, + PickerDemoType.time => localizations.demoTimePickerTitle, + PickerDemoType.range => localizations.demoDateRangePickerTitle, + }; } String get _labelText { diff --git a/dev/integration_tests/new_gallery/lib/demos/material/tabs_demo.dart b/dev/integration_tests/new_gallery/lib/demos/material/tabs_demo.dart index be5cf90e20..de79cea0b8 100644 --- a/dev/integration_tests/new_gallery/lib/demos/material/tabs_demo.dart +++ b/dev/integration_tests/new_gallery/lib/demos/material/tabs_demo.dart @@ -13,14 +13,10 @@ class TabsDemo extends StatelessWidget { @override Widget build(BuildContext context) { - Widget tabs; - switch (type) { - case TabsDemoType.scrollable: - tabs = _TabsScrollableDemo(); - case TabsDemoType.nonScrollable: - tabs = _TabsNonScrollableDemo(); - } - return tabs; + return switch (type) { + TabsDemoType.scrollable => _TabsScrollableDemo(), + TabsDemoType.nonScrollable => _TabsNonScrollableDemo(), + }; } } diff --git a/dev/integration_tests/new_gallery/lib/pages/demo.dart b/dev/integration_tests/new_gallery/lib/pages/demo.dart index ee9bf74400..46ad1ecc25 100644 --- a/dev/integration_tests/new_gallery/lib/pages/demo.dart +++ b/dev/integration_tests/new_gallery/lib/pages/demo.dart @@ -433,16 +433,11 @@ class _GalleryDemoPageState extends State page = AnimatedBuilder( animation: _codeBackgroundColorController, builder: (BuildContext context, Widget? child) { - Brightness themeBrightness; - - switch (GalleryOptions.of(context).themeMode) { - case ThemeMode.system: - themeBrightness = MediaQuery.of(context).platformBrightness; - case ThemeMode.light: - themeBrightness = Brightness.light; - case ThemeMode.dark: - themeBrightness = Brightness.dark; - } + final Brightness themeBrightness = switch (GalleryOptions.of(context).themeMode) { + ThemeMode.system => MediaQuery.of(context).platformBrightness, + ThemeMode.light => Brightness.light, + ThemeMode.dark => Brightness.dark, + }; Widget contents = Container( padding: EdgeInsets.symmetric(horizontal: horizontalPadding), diff --git a/dev/integration_tests/new_gallery/lib/studies/reply/app.dart b/dev/integration_tests/new_gallery/lib/studies/reply/app.dart index f27fb0b130..7310d676d9 100644 --- a/dev/integration_tests/new_gallery/lib/studies/reply/app.dart +++ b/dev/integration_tests/new_gallery/lib/studies/reply/app.dart @@ -89,17 +89,13 @@ class _ReplyAppState extends State with RestorationMixin { supportedLocales: GalleryLocalizations.supportedLocales, locale: GalleryOptions.of(context).locale, initialRoute: ReplyApp.homeRoute, - onGenerateRoute: (RouteSettings settings) { - switch (settings.name) { - case ReplyApp.homeRoute: - return MaterialPageRoute( - builder: (BuildContext context) => const AdaptiveNav(), - settings: settings, - ); - case ReplyApp.composeRoute: - return ReplyApp.createComposeRoute(settings); - } - return null; + onGenerateRoute: (RouteSettings settings) => switch (settings.name) { + ReplyApp.homeRoute => MaterialPageRoute( + builder: (BuildContext context) => const AdaptiveNav(), + settings: settings, + ), + ReplyApp.composeRoute => ReplyApp.createComposeRoute(settings), + _ => null, }, ), ); diff --git a/dev/integration_tests/new_gallery/lib/studies/reply/mailbox_body.dart b/dev/integration_tests/new_gallery/lib/studies/reply/mailbox_body.dart index 75b59999e3..2e8316ceb7 100644 --- a/dev/integration_tests/new_gallery/lib/studies/reply/mailbox_body.dart +++ b/dev/integration_tests/new_gallery/lib/studies/reply/mailbox_body.dart @@ -34,40 +34,15 @@ class MailboxBody extends StatelessWidget { final String destinationString = destination .toString() .substring(destination.toString().indexOf('.') + 1); - late List emails; - switch (destination) { - case MailboxPageType.inbox: - { - emails = model.inboxEmails; - break; - } - case MailboxPageType.sent: - { - emails = model.outboxEmails; - break; - } - case MailboxPageType.starred: - { - emails = model.starredEmails; - break; - } - case MailboxPageType.trash: - { - emails = model.trashEmails; - break; - } - case MailboxPageType.spam: - { - emails = model.spamEmails; - break; - } - case MailboxPageType.drafts: - { - emails = model.draftEmails; - break; - } - } + final List emails = switch (destination) { + MailboxPageType.inbox => model.inboxEmails, + MailboxPageType.sent => model.outboxEmails, + MailboxPageType.starred => model.starredEmails, + MailboxPageType.trash => model.trashEmails, + MailboxPageType.spam => model.spamEmails, + MailboxPageType.drafts => model.draftEmails, + }; return SafeArea( bottom: false, diff --git a/dev/integration_tests/platform_interaction/lib/src/test_step.dart b/dev/integration_tests/platform_interaction/lib/src/test_step.dart index 4bd004b5fe..e2fbde0c23 100644 --- a/dev/integration_tests/platform_interaction/lib/src/test_step.dart +++ b/dev/integration_tests/platform_interaction/lib/src/test_step.dart @@ -16,21 +16,12 @@ class TestStepResult { const TestStepResult(this.name, this.description, this.status); factory TestStepResult.fromSnapshot(AsyncSnapshot snapshot) { - switch (snapshot.connectionState) { - case ConnectionState.none: - return const TestStepResult('Not started', nothing, TestStatus.ok); - case ConnectionState.waiting: - return const TestStepResult('Executing', nothing, TestStatus.pending); - case ConnectionState.done: - if (snapshot.hasData) { - return snapshot.data!; - } else { - final Object? result = snapshot.error; - return result! as TestStepResult; - } - case ConnectionState.active: - throw 'Unsupported state ${snapshot.connectionState}'; - } + return switch (snapshot.connectionState) { + ConnectionState.none => const TestStepResult('Not started', nothing, TestStatus.ok), + ConnectionState.waiting => const TestStepResult('Executing', nothing, TestStatus.pending), + ConnectionState.done => snapshot.data ?? snapshot.error! as TestStepResult, + ConnectionState.active => throw 'Unsupported state: ConnectionState.active', + }; } final String name; diff --git a/dev/snippets/lib/src/data_types.dart b/dev/snippets/lib/src/data_types.dart index 4519107622..f19f9e0003 100644 --- a/dev/snippets/lib/src/data_types.dart +++ b/dev/snippets/lib/src/data_types.dart @@ -325,26 +325,18 @@ enum SourceElementType { unknownType, } -/// Converts the enun type [SourceElementType] to a human readable string. +/// Converts the enum type [SourceElementType] to a human readable string. String sourceElementTypeAsString(SourceElementType type) { - switch (type) { - case SourceElementType.classType: - return 'class'; - case SourceElementType.fieldType: - return 'field'; - case SourceElementType.methodType: - return 'method'; - case SourceElementType.constructorType: - return 'constructor'; - case SourceElementType.typedefType: - return 'typedef'; - case SourceElementType.topLevelVariableType: - return 'variable'; - case SourceElementType.functionType: - return 'function'; - case SourceElementType.unknownType: - return 'unknown'; - } + return switch (type) { + SourceElementType.classType => 'class', + SourceElementType.fieldType => 'field', + SourceElementType.methodType => 'method', + SourceElementType.constructorType => 'constructor', + SourceElementType.typedefType => 'typedef', + SourceElementType.topLevelVariableType => 'variable', + SourceElementType.functionType => 'function', + SourceElementType.unknownType => 'unknown', + }; } /// A class that represents a Dart element in a source file. diff --git a/dev/tools/localization/localizations_utils.dart b/dev/tools/localization/localizations_utils.dart index 6c3d2e1932..e0f18f75ed 100644 --- a/dev/tools/localization/localizations_utils.dart +++ b/dev/tools/localization/localizations_utils.dart @@ -62,29 +62,12 @@ class LocaleInfo implements Comparable { /// across various countries. For example, we know Taiwan uses traditional (Hant) /// script, so it is safe to apply (Hant) to Taiwanese languages. if (deriveScriptCode && scriptCode == null) { - switch (languageCode) { - case 'zh': { - if (countryCode == null) { - scriptCode = 'Hans'; - } - switch (countryCode) { - case 'CN': - case 'SG': - scriptCode = 'Hans'; - case 'TW': - case 'HK': - case 'MO': - scriptCode = 'Hant'; - } - break; - } - case 'sr': { - if (countryCode == null) { - scriptCode = 'Cyrl'; - } - break; - } - } + scriptCode = switch ((languageCode, countryCode)) { + ('zh', 'CN' || 'SG' || null) => 'Hans', + ('zh', 'TW' || 'HK' || 'MO') => 'Hant', + ('sr', null) => 'Cyrl', + _ => null, + }; // Increment length if we were able to assume a scriptCode. if (scriptCode != null) { length += 1; diff --git a/packages/flutter/lib/src/cupertino/text_selection.dart b/packages/flutter/lib/src/cupertino/text_selection.dart index 8d9d258d91..684d01218e 100644 --- a/packages/flutter/lib/src/cupertino/text_selection.dart +++ b/packages/flutter/lib/src/cupertino/text_selection.dart @@ -155,13 +155,12 @@ class CupertinoTextSelectionControls extends TextSelectionControls { /// See [TextSelectionControls.getHandleAnchor]. @override Offset getHandleAnchor(TextSelectionHandleType type, double textLineHeight) { - final Size handleSize; + final Size handleSize = getHandleSize(textLineHeight); switch (type) { // The circle is at the top for the left handle, and the anchor point is // all the way at the bottom of the line. case TextSelectionHandleType.left: - handleSize = getHandleSize(textLineHeight); return Offset( handleSize.width / 2, handleSize.height, @@ -169,14 +168,12 @@ class CupertinoTextSelectionControls extends TextSelectionControls { // The right handle is vertically flipped, and the anchor point is near // the top of the circle to give slight overlap. case TextSelectionHandleType.right: - handleSize = getHandleSize(textLineHeight); return Offset( handleSize.width / 2, handleSize.height - 2 * _kSelectionHandleRadius + _kSelectionHandleOverlap, ); // A collapsed handle anchors itself so that it's centered. case TextSelectionHandleType.collapsed: - handleSize = getHandleSize(textLineHeight); return Offset( handleSize.width / 2, textLineHeight + (handleSize.height - textLineHeight) / 2, diff --git a/packages/flutter/lib/src/cupertino/text_selection_toolbar_button.dart b/packages/flutter/lib/src/cupertino/text_selection_toolbar_button.dart index 05b95c187a..06723a6b2e 100644 --- a/packages/flutter/lib/src/cupertino/text_selection_toolbar_button.dart +++ b/packages/flutter/lib/src/cupertino/text_selection_toolbar_button.dart @@ -174,10 +174,7 @@ class _CupertinoTextSelectionToolbarButtonState extends State curvedAnimation) { - return builder(context, curvedAnimation); - }, + builder: builder, ); } } diff --git a/packages/flutter/lib/src/widgets/drag_target.dart b/packages/flutter/lib/src/widgets/drag_target.dart index 8cc341582e..5942683887 100644 --- a/packages/flutter/lib/src/widgets/drag_target.dart +++ b/packages/flutter/lib/src/widgets/drag_target.dart @@ -370,14 +370,11 @@ class Draggable extends StatefulWidget { /// recognizing a drag. @protected MultiDragGestureRecognizer createRecognizer(GestureMultiDragStartCallback onStart) { - switch (affinity) { - case Axis.horizontal: - return HorizontalMultiDragGestureRecognizer(allowedButtonsFilter: allowedButtonsFilter)..onStart = onStart; - case Axis.vertical: - return VerticalMultiDragGestureRecognizer(allowedButtonsFilter: allowedButtonsFilter)..onStart = onStart; - case null: - return ImmediateMultiDragGestureRecognizer(allowedButtonsFilter: allowedButtonsFilter)..onStart = onStart; - } + return switch (affinity) { + Axis.horizontal => HorizontalMultiDragGestureRecognizer(allowedButtonsFilter: allowedButtonsFilter), + Axis.vertical => VerticalMultiDragGestureRecognizer(allowedButtonsFilter: allowedButtonsFilter), + null => ImmediateMultiDragGestureRecognizer(allowedButtonsFilter: allowedButtonsFilter), + }..onStart = onStart; } @override diff --git a/packages/flutter/lib/src/widgets/scroll_physics.dart b/packages/flutter/lib/src/widgets/scroll_physics.dart index b3a6ec8b22..b264417f63 100644 --- a/packages/flutter/lib/src/widgets/scroll_physics.dart +++ b/packages/flutter/lib/src/widgets/scroll_physics.dart @@ -675,12 +675,10 @@ class BouncingScrollPhysics extends ScrollPhysics { /// as more of the area past the edge is dragged in (represented by an increasing /// `overscrollFraction` which starts at 0 when there is no overscroll). double frictionFactor(double overscrollFraction) { - switch (decelerationRate) { - case ScrollDecelerationRate.fast: - return 0.26 * math.pow(1 - overscrollFraction, 2); - case ScrollDecelerationRate.normal: - return 0.52 * math.pow(1 - overscrollFraction, 2); - } + return math.pow(1 - overscrollFraction, 2) * switch (decelerationRate) { + ScrollDecelerationRate.fast => 0.26, + ScrollDecelerationRate.normal => 0.52, + }; } @override diff --git a/packages/flutter/lib/src/widgets/scrollbar.dart b/packages/flutter/lib/src/widgets/scrollbar.dart index c0cf60055c..76b0e0f6e4 100644 --- a/packages/flutter/lib/src/widgets/scrollbar.dart +++ b/packages/flutter/lib/src/widgets/scrollbar.dart @@ -1724,16 +1724,14 @@ class RawScrollbarState extends State with TickerProv // Determines the scroll direction. final AxisDirection scrollDirection; - switch (position.axisDirection) { - case AxisDirection.up: - case AxisDirection.down: + switch (axisDirectionToAxis(position.axisDirection)) { + case Axis.vertical: if (details.localPosition.dy > scrollbarPainter._thumbOffset) { scrollDirection = AxisDirection.down; } else { scrollDirection = AxisDirection.up; } - case AxisDirection.left: - case AxisDirection.right: + case Axis.horizontal: if (details.localPosition.dx > scrollbarPainter._thumbOffset) { scrollDirection = AxisDirection.right; } else { diff --git a/packages/flutter_driver/lib/src/common/deserialization_factory.dart b/packages/flutter_driver/lib/src/common/deserialization_factory.dart index 0ad51b7853..6be02b5f43 100644 --- a/packages/flutter_driver/lib/src/common/deserialization_factory.dart +++ b/packages/flutter_driver/lib/src/common/deserialization_factory.dart @@ -22,18 +22,17 @@ import 'wait.dart'; mixin DeserializeFinderFactory { /// Deserializes the finder from JSON generated by [SerializableFinder.serialize]. SerializableFinder deserializeFinder(Map json) { - final String? finderType = json['finderType']; - switch (finderType) { - case 'ByType': return ByType.deserialize(json); - case 'ByValueKey': return ByValueKey.deserialize(json); - case 'ByTooltipMessage': return ByTooltipMessage.deserialize(json); - case 'BySemanticsLabel': return BySemanticsLabel.deserialize(json); - case 'ByText': return ByText.deserialize(json); - case 'PageBack': return const PageBack(); - case 'Descendant': return Descendant.deserialize(json, this); - case 'Ancestor': return Ancestor.deserialize(json, this); - } - throw DriverError('Unsupported search specification type $finderType'); + return switch (json['finderType']) { + 'ByType' => ByType.deserialize(json), + 'ByValueKey' => ByValueKey.deserialize(json), + 'ByTooltipMessage' => ByTooltipMessage.deserialize(json), + 'BySemanticsLabel' => BySemanticsLabel.deserialize(json), + 'ByText' => ByText.deserialize(json), + 'PageBack' => const PageBack(), + 'Descendant' => Descendant.deserialize(json, this), + 'Ancestor' => Ancestor.deserialize(json, this), + _ => throw DriverError('Unsupported search specification type ${json['finderType']}'), + }; } } @@ -41,33 +40,31 @@ mixin DeserializeFinderFactory { mixin DeserializeCommandFactory { /// Deserializes the finder from JSON generated by [Command.serialize] or [CommandWithTarget.serialize]. Command deserializeCommand(Map params, DeserializeFinderFactory finderFactory) { - final String? kind = params['command']; - switch (kind) { - case 'get_health': return GetHealth.deserialize(params); - case 'get_layer_tree': return GetLayerTree.deserialize(params); - case 'get_render_tree': return GetRenderTree.deserialize(params); - case 'enter_text': return EnterText.deserialize(params); - case 'send_text_input_action': return SendTextInputAction.deserialize(params); - case 'get_text': return GetText.deserialize(params, finderFactory); - case 'request_data': return RequestData.deserialize(params); - case 'scroll': return Scroll.deserialize(params, finderFactory); - case 'scrollIntoView': return ScrollIntoView.deserialize(params, finderFactory); - case 'set_frame_sync': return SetFrameSync.deserialize(params); - case 'set_semantics': return SetSemantics.deserialize(params); - case 'set_text_entry_emulation': return SetTextEntryEmulation.deserialize(params); - case 'tap': return Tap.deserialize(params, finderFactory); - case 'waitFor': return WaitFor.deserialize(params, finderFactory); - case 'waitForAbsent': return WaitForAbsent.deserialize(params, finderFactory); - case 'waitForTappable': return WaitForTappable.deserialize(params, finderFactory); - case 'waitForCondition': return WaitForCondition.deserialize(params); - case 'waitUntilNoTransientCallbacks': return WaitForCondition.deserialize(params); - case 'waitUntilNoPendingFrame': return WaitForCondition.deserialize(params); - case 'waitUntilFirstFrameRasterized': return WaitForCondition.deserialize(params); - case 'get_semantics_id': return GetSemanticsId.deserialize(params, finderFactory); - case 'get_offset': return GetOffset.deserialize(params, finderFactory); - case 'get_diagnostics_tree': return GetDiagnosticsTree.deserialize(params, finderFactory); - } - - throw DriverError('Unsupported command kind $kind'); + return switch (params['command']) { + 'get_health' => GetHealth.deserialize(params), + 'get_layer_tree' => GetLayerTree.deserialize(params), + 'get_render_tree' => GetRenderTree.deserialize(params), + 'enter_text' => EnterText.deserialize(params), + 'send_text_input_action' => SendTextInputAction.deserialize(params), + 'get_text' => GetText.deserialize(params, finderFactory), + 'request_data' => RequestData.deserialize(params), + 'scroll' => Scroll.deserialize(params, finderFactory), + 'scrollIntoView' => ScrollIntoView.deserialize(params, finderFactory), + 'set_frame_sync' => SetFrameSync.deserialize(params), + 'set_semantics' => SetSemantics.deserialize(params), + 'set_text_entry_emulation' => SetTextEntryEmulation.deserialize(params), + 'tap' => Tap.deserialize(params, finderFactory), + 'waitFor' => WaitFor.deserialize(params, finderFactory), + 'waitForAbsent' => WaitForAbsent.deserialize(params, finderFactory), + 'waitForTappable' => WaitForTappable.deserialize(params, finderFactory), + 'waitForCondition' => WaitForCondition.deserialize(params), + 'waitUntilNoTransientCallbacks' => WaitForCondition.deserialize(params), + 'waitUntilNoPendingFrame' => WaitForCondition.deserialize(params), + 'waitUntilFirstFrameRasterized' => WaitForCondition.deserialize(params), + 'get_semantics_id' => GetSemanticsId.deserialize(params, finderFactory), + 'get_offset' => GetOffset.deserialize(params, finderFactory), + 'get_diagnostics_tree' => GetDiagnosticsTree.deserialize(params, finderFactory), + final String? kind => throw DriverError('Unsupported command kind $kind'), + }; } } diff --git a/packages/flutter_driver/lib/src/common/find.dart b/packages/flutter_driver/lib/src/common/find.dart index 326455a0ba..d2c0e00571 100644 --- a/packages/flutter_driver/lib/src/common/find.dart +++ b/packages/flutter_driver/lib/src/common/find.dart @@ -231,15 +231,11 @@ class ByValueKey extends SerializableFinder { /// Deserializes the finder from JSON generated by [serialize]. static ByValueKey deserialize(Map json) { final String keyValueString = json['keyValueString']!; - final String keyValueType = json['keyValueType']!; - switch (keyValueType) { - case 'int': - return ByValueKey(int.parse(keyValueString)); - case 'String': - return ByValueKey(keyValueString); - default: - throw _createInvalidKeyValueTypeError(keyValueType); - } + return switch (json['keyValueType']!) { + 'int' => ByValueKey(int.parse(keyValueString)), + 'String' => ByValueKey(keyValueString), + final String keyValueType => throw _createInvalidKeyValueTypeError(keyValueType), + }; } } diff --git a/packages/flutter_driver/lib/src/common/handler_factory.dart b/packages/flutter_driver/lib/src/common/handler_factory.dart index 9960e8396d..6afbebe242 100644 --- a/packages/flutter_driver/lib/src/common/handler_factory.dart +++ b/packages/flutter_driver/lib/src/common/handler_factory.dart @@ -33,27 +33,17 @@ import 'wait.dart'; mixin CreateFinderFactory { /// Creates the flutter widget finder from [SerializableFinder]. Finder createFinder(SerializableFinder finder) { - final String finderType = finder.finderType; - switch (finderType) { - case 'ByText': - return _createByTextFinder(finder as ByText); - case 'ByTooltipMessage': - return _createByTooltipMessageFinder(finder as ByTooltipMessage); - case 'BySemanticsLabel': - return _createBySemanticsLabelFinder(finder as BySemanticsLabel); - case 'ByValueKey': - return _createByValueKeyFinder(finder as ByValueKey); - case 'ByType': - return _createByTypeFinder(finder as ByType); - case 'PageBack': - return _createPageBackFinder(); - case 'Ancestor': - return _createAncestorFinder(finder as Ancestor); - case 'Descendant': - return _createDescendantFinder(finder as Descendant); - default: - throw DriverError('Unsupported search specification type $finderType'); - } + return switch (finder.finderType) { + 'ByText' => _createByTextFinder(finder as ByText), + 'ByTooltipMessage' => _createByTooltipMessageFinder(finder as ByTooltipMessage), + 'BySemanticsLabel' => _createBySemanticsLabelFinder(finder as BySemanticsLabel), + 'ByValueKey' => _createByValueKeyFinder(finder as ByValueKey), + 'ByType' => _createByTypeFinder(finder as ByType), + 'PageBack' => _createPageBackFinder(), + 'Ancestor' => _createAncestorFinder(finder as Ancestor), + 'Descendant' => _createDescendantFinder(finder as Descendant), + final String type => throw DriverError('Unsupported search specification type $type'), + }; } Finder _createByTextFinder(ByText arguments) { @@ -87,14 +77,11 @@ mixin CreateFinderFactory { } Finder _createByValueKeyFinder(ByValueKey arguments) { - switch (arguments.keyValueType) { - case 'int': - return find.byKey(ValueKey(arguments.keyValue as int)); - case 'String': - return find.byKey(ValueKey(arguments.keyValue as String)); - default: - throw UnimplementedError('Unsupported ByValueKey type: ${arguments.keyValueType}'); - } + return switch (arguments.keyValueType) { + 'int' => find.byKey(ValueKey(arguments.keyValue as int)), + 'String' => find.byKey(ValueKey(arguments.keyValue as String)), + _ => throw UnimplementedError('Unsupported ByValueKey type: ${arguments.keyValueType}'), + }; } Finder _createByTypeFinder(ByType arguments) { @@ -155,33 +142,33 @@ mixin CommandHandlerFactory { /// Deserializes the finder from JSON generated by [Command.serialize] or [CommandWithTarget.serialize]. Future handleCommand(Command command, WidgetController prober, CreateFinderFactory finderFactory) { - switch (command.kind) { - case 'get_health': return _getHealth(command); - case 'get_layer_tree': return _getLayerTree(command); - case 'get_render_tree': return _getRenderTree(command); - case 'enter_text': return _enterText(command); - case 'send_text_input_action': return _sendTextInputAction(command); - case 'get_text': return _getText(command, finderFactory); - case 'request_data': return _requestData(command); - case 'scroll': return _scroll(command, prober, finderFactory); - case 'scrollIntoView': return _scrollIntoView(command, finderFactory); - case 'set_frame_sync': return _setFrameSync(command); - case 'set_semantics': return _setSemantics(command); - case 'set_text_entry_emulation': return _setTextEntryEmulation(command); - case 'tap': return _tap(command, prober, finderFactory); - case 'waitFor': return _waitFor(command, finderFactory); - case 'waitForAbsent': return _waitForAbsent(command, finderFactory); - case 'waitForTappable': return _waitForTappable(command, finderFactory); - case 'waitForCondition': return _waitForCondition(command); - case 'waitUntilNoTransientCallbacks': return _waitUntilNoTransientCallbacks(command); - case 'waitUntilNoPendingFrame': return _waitUntilNoPendingFrame(command); - case 'waitUntilFirstFrameRasterized': return _waitUntilFirstFrameRasterized(command); - case 'get_semantics_id': return _getSemanticsId(command, finderFactory); - case 'get_offset': return _getOffset(command, finderFactory); - case 'get_diagnostics_tree': return _getDiagnosticsTree(command, finderFactory); - } + return switch (command.kind) { + 'get_health' => _getHealth(command), + 'get_layer_tree' => _getLayerTree(command), + 'get_render_tree' => _getRenderTree(command), + 'enter_text' => _enterText(command), + 'send_text_input_action' => _sendTextInputAction(command), + 'get_text' => _getText(command, finderFactory), + 'request_data' => _requestData(command), + 'scroll' => _scroll(command, prober, finderFactory), + 'scrollIntoView' => _scrollIntoView(command, finderFactory), + 'set_frame_sync' => _setFrameSync(command), + 'set_semantics' => _setSemantics(command), + 'set_text_entry_emulation' => _setTextEntryEmulation(command), + 'tap' => _tap(command, prober, finderFactory), + 'waitFor' => _waitFor(command, finderFactory), + 'waitForAbsent' => _waitForAbsent(command, finderFactory), + 'waitForTappable' => _waitForTappable(command, finderFactory), + 'waitForCondition' => _waitForCondition(command), + 'waitUntilNoTransientCallbacks' => _waitUntilNoTransientCallbacks(command), + 'waitUntilNoPendingFrame' => _waitUntilNoPendingFrame(command), + 'waitUntilFirstFrameRasterized' => _waitUntilFirstFrameRasterized(command), + 'get_semantics_id' => _getSemanticsId(command, finderFactory), + 'get_offset' => _getOffset(command, finderFactory), + 'get_diagnostics_tree' => _getDiagnosticsTree(command, finderFactory), + final String kind => throw DriverError('Unsupported command kind $kind'), + }; - throw DriverError('Unsupported command kind ${command.kind}'); } Future _getHealth(Command command) async => const Health(HealthStatus.ok); @@ -342,19 +329,13 @@ mixin CommandHandlerFactory { final Finder finder = await waitForElement(finderFactory.createFinder(getOffsetCommand.finder)); final Element element = finder.evaluate().single; final RenderBox box = (element.renderObject as RenderBox?)!; - Offset localPoint; - switch (getOffsetCommand.offsetType) { - case OffsetType.topLeft: - localPoint = Offset.zero; - case OffsetType.topRight: - localPoint = box.size.topRight(Offset.zero); - case OffsetType.bottomLeft: - localPoint = box.size.bottomLeft(Offset.zero); - case OffsetType.bottomRight: - localPoint = box.size.bottomRight(Offset.zero); - case OffsetType.center: - localPoint = box.size.center(Offset.zero); - } + final Offset localPoint = switch (getOffsetCommand.offsetType) { + OffsetType.topLeft => Offset.zero, + OffsetType.topRight => box.size.topRight(Offset.zero), + OffsetType.bottomLeft => box.size.bottomLeft(Offset.zero), + OffsetType.bottomRight => box.size.bottomRight(Offset.zero), + OffsetType.center => box.size.center(Offset.zero), + }; final Offset globalPoint = box.localToGlobal(localPoint); return GetOffsetResult(dx: globalPoint.dx, dy: globalPoint.dy); } @@ -363,13 +344,10 @@ mixin CommandHandlerFactory { final GetDiagnosticsTree diagnosticsCommand = command as GetDiagnosticsTree; final Finder finder = await waitForElement(finderFactory.createFinder(diagnosticsCommand.finder)); final Element element = finder.evaluate().single; - DiagnosticsNode diagnosticsNode; - switch (diagnosticsCommand.diagnosticsType) { - case DiagnosticsType.renderObject: - diagnosticsNode = element.renderObject!.toDiagnosticsNode(); - case DiagnosticsType.widget: - diagnosticsNode = element.toDiagnosticsNode(); - } + final DiagnosticsNode diagnosticsNode = switch (diagnosticsCommand.diagnosticsType) { + DiagnosticsType.renderObject => element.renderObject!.toDiagnosticsNode(), + DiagnosticsType.widget => element.toDiagnosticsNode(), + }; return DiagnosticsTreeResult(diagnosticsNode.toJsonMap(DiagnosticsSerializationDelegate( subtreeDepth: diagnosticsCommand.subtreeDepth, includeProperties: diagnosticsCommand.includeProperties, diff --git a/packages/flutter_driver/lib/src/common/wait.dart b/packages/flutter_driver/lib/src/common/wait.dart index 73a3424e13..b3b6201cbb 100644 --- a/packages/flutter_driver/lib/src/common/wait.dart +++ b/packages/flutter_driver/lib/src/common/wait.dart @@ -195,19 +195,14 @@ class CombinedCondition extends SerializableWaitCondition { /// Parses a [SerializableWaitCondition] or its subclass from the given [json] map. SerializableWaitCondition _deserialize(Map json) { - final String conditionName = json['conditionName']!; - switch (conditionName) { - case 'NoTransientCallbacksCondition': - return NoTransientCallbacks.deserialize(json); - case 'NoPendingFrameCondition': - return NoPendingFrame.deserialize(json); - case 'FirstFrameRasterizedCondition': - return FirstFrameRasterized.deserialize(json); - case 'NoPendingPlatformMessagesCondition': - return NoPendingPlatformMessages.deserialize(json); - case 'CombinedCondition': - return CombinedCondition.deserialize(json); - } - throw SerializationException( - 'Unsupported wait condition $conditionName in the JSON string $json'); + return switch (json['conditionName']!) { + 'NoTransientCallbacksCondition' => NoTransientCallbacks.deserialize(json), + 'NoPendingFrameCondition' => NoPendingFrame.deserialize(json), + 'FirstFrameRasterizedCondition' => FirstFrameRasterized.deserialize(json), + 'NoPendingPlatformMessagesCondition' => NoPendingPlatformMessages.deserialize(json), + 'CombinedCondition' => CombinedCondition.deserialize(json), + final String condition => throw SerializationException( + 'Unsupported wait condition $condition in the JSON string $json', + ), + }; } diff --git a/packages/flutter_driver/lib/src/driver/profiling_summarizer.dart b/packages/flutter_driver/lib/src/driver/profiling_summarizer.dart index 09f970ee51..0d4b464164 100644 --- a/packages/flutter_driver/lib/src/driver/profiling_summarizer.dart +++ b/packages/flutter_driver/lib/src/driver/profiling_summarizer.dart @@ -121,16 +121,12 @@ class ProfilingSummarizer { } static ProfileType _getProfileType(String? eventName) { - switch (eventName) { - case _kCpuProfile: - return ProfileType.CPU; - case _kGpuProfile: - return ProfileType.GPU; - case _kMemoryProfile: - return ProfileType.Memory; - default: - throw Exception('Invalid profiling event: $eventName.'); - } + return switch (eventName) { + _kCpuProfile => ProfileType.CPU, + _kGpuProfile => ProfileType.GPU, + _kMemoryProfile => ProfileType.Memory, + _ => throw Exception('Invalid profiling event: $eventName.'), + }; } double _getProfileValue(ProfileType profileType, TimelineEvent e) { diff --git a/packages/flutter_driver/lib/src/driver/vmservice_driver.dart b/packages/flutter_driver/lib/src/driver/vmservice_driver.dart index 99fdc6fcb1..c91596076c 100644 --- a/packages/flutter_driver/lib/src/driver/vmservice_driver.dart +++ b/packages/flutter_driver/lib/src/driver/vmservice_driver.dart @@ -601,19 +601,17 @@ const Duration _kPauseBetweenIsolateRefresh = Duration(milliseconds: 100); // See `timeline_streams` in // https://github.com/dart-lang/sdk/blob/main/runtime/vm/timeline.cc List _timelineStreamsToString(List streams) { - return streams.map((TimelineStream stream) { - switch (stream) { - case TimelineStream.all: return 'all'; - case TimelineStream.api: return 'API'; - case TimelineStream.compiler: return 'Compiler'; - case TimelineStream.compilerVerbose: return 'CompilerVerbose'; - case TimelineStream.dart: return 'Dart'; - case TimelineStream.debugger: return 'Debugger'; - case TimelineStream.embedder: return 'Embedder'; - case TimelineStream.gc: return 'GC'; - case TimelineStream.isolate: return 'Isolate'; - case TimelineStream.vm: return 'VM'; - } + return streams.map((TimelineStream stream) => switch (stream) { + TimelineStream.all => 'all', + TimelineStream.api => 'API', + TimelineStream.dart => 'Dart', + TimelineStream.debugger => 'Debugger', + TimelineStream.embedder => 'Embedder', + TimelineStream.gc => 'GC', + TimelineStream.isolate => 'Isolate', + TimelineStream.vm => 'VM', + TimelineStream.compiler => 'Compiler', + TimelineStream.compilerVerbose => 'CompilerVerbose', }).toList(); } diff --git a/packages/flutter_driver/lib/src/extension/wait_conditions.dart b/packages/flutter_driver/lib/src/extension/wait_conditions.dart index 424a0adf93..7df3c6bac0 100644 --- a/packages/flutter_driver/lib/src/extension/wait_conditions.dart +++ b/packages/flutter_driver/lib/src/extension/wait_conditions.dart @@ -177,18 +177,12 @@ class _InternalCombinedCondition implements WaitCondition { /// Parses a [WaitCondition] or its subclass from the given serializable [waitCondition]. WaitCondition deserializeCondition(SerializableWaitCondition waitCondition) { final String conditionName = waitCondition.conditionName; - switch (conditionName) { - case 'NoTransientCallbacksCondition': - return _InternalNoTransientCallbacksCondition.deserialize(waitCondition); - case 'NoPendingFrameCondition': - return _InternalNoPendingFrameCondition.deserialize(waitCondition); - case 'FirstFrameRasterizedCondition': - return _InternalFirstFrameRasterizedCondition.deserialize(waitCondition); - case 'NoPendingPlatformMessagesCondition': - return _InternalNoPendingPlatformMessagesCondition.deserialize(waitCondition); - case 'CombinedCondition': - return _InternalCombinedCondition.deserialize(waitCondition); - } - throw SerializationException( - 'Unsupported wait condition $conditionName in ${waitCondition.serialize()}'); + return switch (conditionName) { + 'NoTransientCallbacksCondition' => _InternalNoTransientCallbacksCondition.deserialize(waitCondition), + 'NoPendingFrameCondition' => _InternalNoPendingFrameCondition.deserialize(waitCondition), + 'FirstFrameRasterizedCondition' => _InternalFirstFrameRasterizedCondition.deserialize(waitCondition), + 'NoPendingPlatformMessagesCondition' => _InternalNoPendingPlatformMessagesCondition.deserialize(waitCondition), + 'CombinedCondition' => _InternalCombinedCondition.deserialize(waitCondition), + _ => throw SerializationException('Unsupported wait condition $conditionName in ${waitCondition.serialize()}'), + }; } diff --git a/packages/flutter_localizations/lib/src/material_localizations.dart b/packages/flutter_localizations/lib/src/material_localizations.dart index e415db1726..4c6189f374 100644 --- a/packages/flutter_localizations/lib/src/material_localizations.dart +++ b/packages/flutter_localizations/lib/src/material_localizations.dart @@ -226,12 +226,10 @@ abstract class GlobalMaterialLocalizations implements MaterialLocalizations { } String? _formatDayPeriod(TimeOfDay timeOfDay) { - switch (timeOfDay.period) { - case DayPeriod.am: - return anteMeridiemAbbreviation; - case DayPeriod.pm: - return postMeridiemAbbreviation; - } + return switch (timeOfDay.period) { + DayPeriod.am => anteMeridiemAbbreviation, + DayPeriod.pm => postMeridiemAbbreviation, + }; } /// The raw version of [dateRangeStartDateSemanticLabel], with `$formattedDate` verbatim diff --git a/packages/flutter_test/lib/src/test_default_binary_messenger.dart b/packages/flutter_test/lib/src/test_default_binary_messenger.dart index 25647bc9d9..de18e74191 100644 --- a/packages/flutter_test/lib/src/test_default_binary_messenger.dart +++ b/packages/flutter_test/lib/src/test_default_binary_messenger.dart @@ -353,16 +353,14 @@ class TestDefaultBinaryMessenger extends BinaryMessenger { final StreamController controller = StreamController(); addTearDown(controller.close); - setMockMethodCallHandler(MethodChannel(channel.name, channel.codec), (MethodCall call) async { - switch (call.method) { - case 'listen': - return handler.onListen(call.arguments, MockStreamHandlerEventSink(controller.sink)); - case 'cancel': - return handler.onCancel(call.arguments); - default: - throw UnimplementedError('Method ${call.method} not implemented'); - } - }); + setMockMethodCallHandler( + MethodChannel(channel.name, channel.codec), + (MethodCall call) async => switch (call.method) { + 'listen' => handler.onListen(call.arguments, MockStreamHandlerEventSink(controller.sink)), + 'cancel' => handler.onCancel(call.arguments), + _ => throw UnimplementedError('Method ${call.method} not implemented'), + }, + ); final StreamSubscription sub = controller.stream.listen( (Object? e) => handlePlatformMessage( diff --git a/packages/flutter_tools/lib/src/flutter_project_metadata.dart b/packages/flutter_tools/lib/src/flutter_project_metadata.dart index 7a9284c2f2..85c59e6f11 100644 --- a/packages/flutter_tools/lib/src/flutter_project_metadata.dart +++ b/packages/flutter_tools/lib/src/flutter_project_metadata.dart @@ -76,9 +76,7 @@ enum FlutterProjectType implements CliEnum { static List get enabledValues { return [ for (final FlutterProjectType value in values) - if (value == FlutterProjectType.packageFfi) ...[ - if (featureFlags.isNativeAssetsEnabled) value - ] else + if (value != FlutterProjectType.packageFfi || featureFlags.isNativeAssetsEnabled) value, ]; } diff --git a/packages/integration_test/lib/common.dart b/packages/integration_test/lib/common.dart index ff2906c6c1..9f94b4512b 100644 --- a/packages/integration_test/lib/common.dart +++ b/packages/integration_test/lib/common.dart @@ -206,16 +206,12 @@ class DriverTestMessage { /// Return a DriverTestMessage depending on `status`. static DriverTestMessage fromString(String status) { - switch (status) { - case 'error': - return DriverTestMessage.error(); - case 'pending': - return DriverTestMessage.pending(); - case 'complete': - return DriverTestMessage.complete(); - default: - throw StateError('This type of status does not exist: $status'); - } + return switch (status) { + 'error' => DriverTestMessage.error(), + 'pending' => DriverTestMessage.pending(), + 'complete' => DriverTestMessage.complete(), + _ => throw StateError('This type of status does not exist: $status'), + }; } }