How to Set Up and Use Flutter in GitHub Actions

Jun 23, 2022 | Programming

Are you away from code, but still want your Flutter app to build smoothly? Don’t worry! Setting up the Flutter environment in GitHub Actions can automate your builds across various platforms like Linux, Windows, and macOS. Here’s a straightforward guide to help you get set up!

Specifying Flutter Version

Flutter can be configured to use a specific version or version from your project’s configuration file. Below are the methods for specifying the Flutter version.

Use Specific Version and Channel

To make sure you’re using a specific version, you can set the Flutter version and channel in your YAML configuration. Think of it like ordering a specific type of coffee at your local café — you want the best version served!

yaml
steps:
  - name: Clone repository
    uses: actions/checkout@v4
  - name: Set up Flutter
    uses: subosito/flutter-action@v2
    with:
      channel: stable
      flutter-version: 3.19.0
  - run: flutter --version

Use Version from pubspec.yaml

If you already have the Flutter version defined in your pubspec.yaml, you can easily have GitHub Actions read from it:

yaml
steps:
  - name: Clone repository
    uses: actions/checkout@v4
  - name: Set up Flutter
    uses: subosito/flutter-action@v2
    with:
      channel: stable
      flutter-version-file: pubspec.yaml # path to pubspec.yaml
  - run: flutter --version

[!IMPORTANT] For the flutter-version-file to work, ensure the exact Flutter version is defined in your pubspec.yaml.

Building Your Flutter App

With Flutter set up, you can now build your Flutter application for various platforms.

Build for Android

To build your Flutter app for Android, follow this procedure:

yaml
steps:
  - name: Clone repository
    uses: actions/checkout@v4
  - name: Set up Flutter
    uses: subosito/flutter-action@v2
    with:
      flutter-version: 3.19.0
  - run: flutter pub get
  - run: flutter test
  - run: flutter build apk
  - run: flutter build appbundle

Build for iOS

One thing to remember: building for iOS requires a macOS runner. Here’s how to set it up:

yaml
jobs:
  main:
    runs-on: macos-latest
    steps:
      - name: Clone repository
        uses: actions/checkout@v4
      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: stable
      - run: flutter pub get
      - run: flutter test
      - run: flutter build ios --release --no-codesign

Caching to Speed Up Builds

If your builds take a while, consider setting up caching to speed things up. It’s like saving your progress on a video game, allowing you to jump right into the fun next time!

yaml
steps:
  - name: Set up Flutter
    uses: subosito/flutter-action@v2
    with:
      channel: stable
      cache: true
  - run: flutter --version

Troubleshooting Tips

  • If your build fails, ensure that the channel and version of Flutter are properly set in the configuration.
  • Make sure your pubspec.yaml is correctly formatted, especially the Flutter version.
  • If you encounter issues using the flutter-version-file option, remember to install yq if you are on Windows, as it is not pre-installed.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox