You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This pull request updates the style guide for improved internal consistency and to match current linter rules.
I'll make comments for each change here!
However, where possible avoid global constants. Rather than `kDefaultButtonColor`, consider `Button.defaultColor`. If necessary, consider creating a class with a private constructor to hold relevant constants.
1077
+
However, where possible avoid global constants. Rather than `kDefaultButtonColor`, consider `Button.defaultColor`. If necessary, consider creating an `abstract final class` to hold relevant constants.
1077
1078
1078
1079
1079
1080
### Avoid abbreviations
@@ -1249,7 +1250,9 @@ include a link to that issue in the code.
1249
1250
Generally the closure passed to `setState` should include all the code that changes the state. Sometimes this is not possible because the state changed elsewhere and the `setState` is called in response. In those cases, include a comment in the `setState` closure that explains what the state is that changed.
1250
1251
1251
1252
```dart
1252
-
setState(() { /* The animation ticked. We use the animation's value in the build method. */ });
1253
+
setState(() {
1254
+
// The animation ticked. We use the animation's value in the build method.
### Use a trailing comma for arguments, parameters, and list items, but only if they each have their own line.
@@ -1453,6 +1450,42 @@ foo1(
1453
1450
foo2(bar, baz);
1454
1451
```
1455
1452
1453
+
If one of the items is a multi-line callback, collection literal,
1454
+
or switch expression, it can be added without a trailing comma.
1455
+
1456
+
```dart
1457
+
// GOOD:
1458
+
foo(
1459
+
bar,
1460
+
baz,
1461
+
switch (value) {
1462
+
true => ScrollDirection.forward,
1463
+
false => ScrollDirection.reverse,
1464
+
null => ScrollDirection.idle,
1465
+
},
1466
+
);
1467
+
1468
+
// also GOOD:
1469
+
foo(bar, baz, switch (value) {
1470
+
true => ScrollDirection.forward,
1471
+
false => ScrollDirection.reverse,
1472
+
null => ScrollDirection.idle,
1473
+
});
1474
+
1475
+
// The same applies to collection literals and callbacks:
1476
+
foo(<String>[
1477
+
'list item 1',
1478
+
'list item 2',
1479
+
'list item 3',
1480
+
]);
1481
+
1482
+
Future.delayed(Durations.short1, () {
1483
+
if (mounted && _shouldOpenDrawer) {
1484
+
_drawerController.forward();
1485
+
}
1486
+
});
1487
+
```
1488
+
1456
1489
Whether to put things all on one line or whether to have one line per item is an aesthetic choice. We prefer whatever ends up being most readable. Typically this means that when everything would fit on one line, put it all on one line, otherwise, split it one item to a line.
1457
1490
1458
1491
However, there are exceptions. For example, if there are six back-to-back lists and all but one of them need multiple lines, then one would not want to have the single case that does fit on one line use a different style than the others.
@@ -1488,17 +1521,6 @@ However, there are exceptions. For example, if there are six back-to-back lists
1488
1521
];
1489
1522
```
1490
1523
1491
-
### Prefer single quotes for strings
1492
-
1493
-
Use double quotes for nested strings or (optionally) for strings that contain single quotes.
1494
-
For all other strings, use single quotes.
1495
-
1496
-
Example:
1497
-
1498
-
```dart
1499
-
print('Hello ${name.split(" ")[0]}');
1500
-
```
1501
-
1502
1524
1503
1525
### Consider using `=>` for short functions and methods
@@ -1666,60 +1723,6 @@ final List<String> args = <String>[
1666
1723
Use a block (with braces) when a body would wrap onto more than one line (as opposed to using `=>`; the cases where you can use `=>` are discussed in the previous two guidelines).
1667
1724
1668
1725
1669
-
### Separate the 'if' expression from its statement
1670
-
1671
-
(This is enforced by the `always_put_control_body_on_new_line` and `curly_braces_in_flow_control_structures` lints.)
1672
-
1673
-
Don't put the statement part of an 'if' statement on the same line as
1674
-
the expression, even if it is short. (Doing so makes it unobvious that
1675
-
there is relevant code there. This is especially important for early
1676
-
returns.)
1677
-
1678
-
Example:
1679
-
1680
-
```dart
1681
-
// BAD:
1682
-
if (notReady) return;
1683
-
1684
-
// GOOD:
1685
-
// Use this style for code that is expected to be publicly read by developers
1686
-
if (notReady) {
1687
-
return;
1688
-
}
1689
-
```
1690
-
1691
-
If the body is more than one line, or if there is an `else` clause, wrap the body in braces:
1692
-
1693
-
```dart
1694
-
// BAD:
1695
-
if (foo)
1696
-
bar(
1697
-
'baz',
1698
-
);
1699
-
1700
-
// BAD:
1701
-
if (foo)
1702
-
bar();
1703
-
else
1704
-
baz();
1705
-
1706
-
// GOOD:
1707
-
if (foo) {
1708
-
bar(
1709
-
'baz',
1710
-
);
1711
-
}
1712
-
1713
-
// GOOD:
1714
-
if (foo) {
1715
-
bar();
1716
-
} else {
1717
-
baz();
1718
-
}
1719
-
```
1720
-
1721
-
We require bodies to make it very clear where the bodies belong.
1722
-
1723
1726
### Align expressions
1724
1727
1725
1728
Where possible, subexpressions on different lines should be aligned, to make the structure of the expression easier. When doing this with a `return` statement chaining `||` or `&&` operators, consider putting the operators on the left hand side instead of the right hand side.
0 commit comments