How to keep your Flutter project clean?
You need to take some actions to keep your codebase clean and maintainable. While we are working on projects as a team, knowledge and coding habits of each team member may vary. We need some coding standards and approaches to keep everyone on the similar level. So, each team member can easily adapt to new features added by others. While working on a new feature, we should switch to a new branch and implement our new feature. Then, while merging our new branch to dev or main branches, it should pass some tests to keep our code base clean. As the Uncle Bob said, making messes is always slower than staying clean, no matter which time scale you are using [1]. Later means never and once the codebase getting messy, everything will be getting worse every day. Checking each PR for code formatting and following lint rules may waste a lot of time. We can reduce this time by implementing GitHub actions on our Flutter projects. All we need to do is, creating a .github folder in our project root directory and put workflows folder in it. Then, we should create a file with yaml extension.
You can copy paste following code to check your PRs on main for analysing code formatting and performing static analysis on the Dart code. Further, you can also check tests automatically with updating pr-main.yaml file.
<code>
name: CI Analysis on PR to main
on:
pull_request:
branches: [main]
env:
flutter_channel: "stable"
flutter_version: "3.7.1" # you may change the version
jobs:
# Analyze and test each package.
analyze-and-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
include:
- os: ubuntu-latest
flutter_path: /opt/hostedtoolcache/flutter
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Cache Flutter dependencies
uses: actions/cache@v1
with:
path: ${{ matrix.flutter_path }}
key: ${{ runner.OS }}-flutter-cache-${{ env.flutter_version }}
restore-keys: |
${{ runner.OS }}-flutter-cache-
- uses: subosito/flutter-action@v2.6.1
with:
channel: ${{ env.flutter_channel }}
flutter-version: ${{ env.flutter_version }}
# Get the latest dependencies
- run: flutter pub get
# check formatting
- name: Check code formatting
run: flutter format --set-exit-if-changed --dry-run .
# check code analysis
- name: Analyze dart code
run: flutter analyze
Using GitHub actions is like having a magical cleaning crew for your codebase, it keeps everything neat, tidy, and organized with just a few clicks! Plus, it’s the ultimate productivity hack that will have you saying ‘goodbye’ to messy code and ‘hello’ to success in no time. So don’t wait until your codebase looks like a tornado hit it take action with GitHub actions today!
- References
- [1] Martin Robert — Clean Architecture_ a Craftsmans Guide to Software Structure and Design, P10.