Skip to content

Commit a0b6113

Browse files
authored
Update style guide for Dart 3.13 (flutter#192639)
Part of flutter#191628 Adds new language features to the style guide ahead of bumping Dart ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 93aa03c commit a0b6113

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

‎docs/contributing/Style-guide-for-Flutter-repo.md‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,82 @@ void foo() {
13431343
```
13441344

13451345

1346+
### Use private named parameters as initializing formals
1347+
1348+
Prefer using private named parameters as initializing formals when initializing private fields.
1349+
1350+
```dart
1351+
// GOOD
1352+
class Point {
1353+
Point({required this._x});
1354+
final double _x;
1355+
}
1356+
1357+
// BAD
1358+
class Point {
1359+
Point({required double x}) : _x = x;
1360+
final double _x;
1361+
}
1362+
```
1363+
1364+
1365+
### Do not use primary constructors in framework widgets
1366+
1367+
Do not use primary constructors to implement Flutter framework widgets.
1368+
1369+
While primary constructors reduce boilerplate for simple classes, they scale poorly to complex
1370+
framework widgets with many fields, assertions, and documentation comments.
1371+
1372+
Primary constructors are permitted in non-widget framework helper classes (e.g., `_TableElementRow`),
1373+
templates, code samples, and documentation. Even for non-widget classes, prefer standard constructors
1374+
if parameters require extensive documentation or complex assertions. App developers are encouraged to
1375+
use primary constructors for their own widgets.
1376+
1377+
```dart
1378+
// GOOD
1379+
class BookingHeader extends StatelessWidget {
1380+
const BookingHeader({
1381+
super.key,
1382+
required this.booking,
1383+
});
1384+
final Booking booking;
1385+
}
1386+
1387+
// BAD (Framework widgets should not use primary header constructors)
1388+
class const BookingHeader({
1389+
super.key,
1390+
required final Booking booking,
1391+
}) extends StatelessWidget {}
1392+
```
1393+
1394+
1395+
### Do not use the new in-body constructor syntax
1396+
1397+
Avoid using the new in-body constructor syntax (`new() {}`, `new name() {}`) introduced in
1398+
Dart 3.13. This syntax is prohibited in the Flutter framework style guide until May 2027 to ensure
1399+
backward compatibility and protect developers on older SDKs.
1400+
1401+
```dart
1402+
// GOOD
1403+
class LongClassName {
1404+
LongClassName() {}
1405+
LongClassName.name() {}
1406+
const LongClassName();
1407+
const LongClassName.name();
1408+
factory LongClassName() = D;
1409+
}
1410+
1411+
// BAD (Prohibited until May 2027)
1412+
class LongClassName {
1413+
new() {}
1414+
new name() {}
1415+
const new();
1416+
const new name();
1417+
factory() = D;
1418+
}
1419+
```
1420+
1421+
13461422
## Writing tests
13471423

13481424
### Make each test entirely self-contained

0 commit comments

Comments
 (0)