Implementing control flow collections (#146601)

This pull request aims for improved readability, based on issue #146600.

```dart
// before
Set<Color> _distinctVisibleColors() {
  final Set<Color> distinctVisibleColors = <Color>{};
  if (top.style != BorderStyle.none) {
    distinctVisibleColors.add(top.color);
  }
  if (right.style != BorderStyle.none) {
    distinctVisibleColors.add(right.color);
  }
  if (bottom.style != BorderStyle.none) {
    distinctVisibleColors.add(bottom.color);
  }
  if (left.style != BorderStyle.none) {
    distinctVisibleColors.add(left.color);
  }
  return distinctVisibleColors;
}

// after
Set<Color> _distinctVisibleColors() {
  return <Color>{
    if (top.style != BorderStyle.none) top.color,
    if (right.style != BorderStyle.none) right.color,
    if (bottom.style != BorderStyle.none) bottom.color,
    if (left.style != BorderStyle.none) left.color,
  };
}
```

Most of the repo should be covered in this PR (aside from `flutter_tools/`, since there was a lot going on in there).
This commit is contained in:
Nate
2024-04-15 11:06:07 -05:00
committed by GitHub
parent 600787891a
commit 2e748e8598
37 changed files with 276 additions and 494 deletions

View File

@@ -154,14 +154,11 @@ class AndroidSemanticsNode {
if (actions == null) {
return const <AndroidSemanticsAction>[];
}
final List<AndroidSemanticsAction> convertedActions = <AndroidSemanticsAction>[];
for (final int id in actions) {
final AndroidSemanticsAction? action = AndroidSemanticsAction.deserialize(id);
if (action != null) {
convertedActions.add(action);
}
}
return convertedActions;
return <AndroidSemanticsAction>[
for (final int id in actions)
if (AndroidSemanticsAction.deserialize(id) case final AndroidSemanticsAction action)
action,
];
}
@override

View File

@@ -121,10 +121,8 @@ class CalcExpression {
: this(<ExpressionToken>[], ExpressionState.Start);
CalcExpression.result(FloatToken result)
: _list = <ExpressionToken?>[],
state = ExpressionState.Result {
_list.add(result);
}
: _list = <ExpressionToken?>[result],
state = ExpressionState.Result;
/// The tokens comprising the expression.
final List<ExpressionToken?> _list;

View File

@@ -84,15 +84,11 @@ class _CupertinoSearchTextFieldDemoState
Widget _buildPlatformList() {
if (_searchPlatform.isNotEmpty) {
final List<String> tempList = <String>[];
for (int i = 0; i < filteredPlatforms.length; i++) {
if (filteredPlatforms[i]
.toLowerCase()
.contains(_searchPlatform.toLowerCase())) {
tempList.add(filteredPlatforms[i]);
}
}
filteredPlatforms = tempList;
final String search = _searchPlatform.toLowerCase();
filteredPlatforms = <String>[
for (final String platform in filteredPlatforms)
if (platform.toLowerCase().contains(search)) platform
];
}
return ListView.builder(
itemCount: filteredPlatforms.length,

View File

@@ -26,14 +26,10 @@ class _RestorableDessertSelections extends RestorableProperty<Set<int>> {
/// Takes a list of [_Dessert]s and saves the row indices of selected rows
/// into a [Set].
void setDessertSelections(List<_Dessert> desserts) {
final Set<int> updatedSet = <int>{};
for (int i = 0; i < desserts.length; i += 1) {
final _Dessert dessert = desserts[i];
if (dessert.selected) {
updatedSet.add(i);
}
}
_dessertSelections = updatedSet;
_dessertSelections = <int>{
for (final (int i, _Dessert dessert) in desserts.indexed)
if (dessert.selected) i,
};
notifyListeners();
}