|
| 1 | +# Flutter Codelabs repository |
| 2 | + |
| 3 | +This repository contains the code for Flutter codelabs. Each project has a copy |
| 4 | +of a project at multiple different steps of the codelab. |
| 5 | + |
| 6 | +## Coding standards |
| 7 | + |
| 8 | +### Formatting & Style |
| 9 | + |
| 10 | +* **`dart format`**: The official Dart formatter is the standard. Most IDEs can be configured to run this automatically on save. |
| 11 | +* **Line Length**: While the default is 80 characters, many projects use up to 120. Consistency is key. |
| 12 | +* **Trailing Commas**: Use trailing commas on the last item in a parameter or argument list. This improves readability and makes for cleaner version control diffs. |
| 13 | +* **Linter**: The `flutter_lints` package is the official linter. It helps enforce style, identify potential errors, and improve code quality. It should be included in your `pubspec.yaml`. |
| 14 | + |
| 15 | +### Naming Conventions |
| 16 | + |
| 17 | +* **`UpperCamelCase`**: For classes, enums, typedefs, and extensions. |
| 18 | +* **`lowerCamelCase`**: For variables, constants, methods, and parameters. |
| 19 | +* **`snake_case`**: For files, directories, and packages. |
| 20 | +* **Private Members**: Use a leading underscore (`_`) for private members of a library (e.g., `_myPrivateVariable`). |
| 21 | + |
| 22 | +### Code Organization |
| 23 | + |
| 24 | +* **DRY (Don't Repeat Yourself)**: Abstract reusable code into widgets, functions, or classes. |
| 25 | +* **Split Large Widgets**: Break down large `build` methods into smaller, more manageable widgets. This improves readability and performance. |
| 26 | +* **Directory Structure**: A common and effective structure is to separate code by feature or layer: |
| 27 | + * `lib/src`: For internal library code. |
| 28 | + * `lib/src/models`: Data classes. |
| 29 | + * `lib/src/widgets` or `lib/src/views`: UI components. |
| 30 | + * `lib/src/controllers` or `lib/src/blocs`: Business logic. |
| 31 | + * `lib/src/services`: API clients, database access, etc. |
| 32 | + |
| 33 | +### Widget Best Practices |
| 34 | + |
| 35 | +* **`const` is King**: Use `const` for widgets and constructors whenever possible. This is a significant performance optimization, as it prevents unnecessary widget rebuilds. |
| 36 | +* **`StatelessWidget` vs. `StatefulWidget`**: Prefer `StatelessWidget` unless you have mutable state that changes over the lifetime of the widget. |
| 37 | +* **`ListView.builder`**: For long or dynamic lists, always use `ListView.builder` to ensure that only the visible items are built and rendered. |
| 38 | +* **Conditional Rendering**: Use collection `if` for conditionally including a widget in a list of children. |
| 39 | + |
| 40 | +### State Management |
| 41 | + |
| 42 | +* While Flutter offers `setState` for local state, for app-level state it's recommended to use a dedicated state management solution like Provider, BLoC, or Riverpod. |
| 43 | +* The key principle is to separate UI from business logic. |
| 44 | + |
| 45 | +### General Dart Best Practices |
| 46 | + |
| 47 | +* **Type Safety**: Avoid `dynamic` when possible. Use strong typing. Use `is` for type checking and avoid `as` to prevent runtime exceptions. |
| 48 | +* **Tear-offs**: If you're calling a function with the same arguments, you can "tear-off" the method instead of creating a new lambda. For example, use `myList.forEach(print)` instead of `myList.forEach((item) => print(item))`. |
| 49 | + |
| 50 | +These standards are widely adopted in the Flutter community and are a great starting point for any project. For more in-depth information, the official [Effective Dart](https://dart.dev/effective-dart) guide is an excellent resource. |
0 commit comments