How to build Flutter snap with build_runner

First time creating a flutter snap.
My flutter app require to run the build_runner for the auto generated files.

flutter pub run build_runner build --delete-conflicting-outputs

Should I add extra part for that to the snapcraft.yaml file?

name: cybear-jinni
version: '1.0'
summary: App to control your smart device through CyBear Jinni Hub
description: Single app to control smart device from different form factor through CyBear Jinni Hub

confinement: devmode

base: core18

parts:
  cybear-jinni:
    plugin: flutter
    source: https://github.com/CyBear-Jinni/CBJ_App.git
    flutter-target: lib/main.dart

apps:
  cybear-jinni:
    command: cybear-jinni
    extensions: [flutter-dev]

Hi,

The flutter plugin doesn’t natively support running commands pre-build with pub run (might be worth pinging @sergiusens or @cjp256 to see if they think it’s something they can add). However, you can add an override-build script that does the command by amending your part to:

  parts:
  cybear-jinni:
    plugin: flutter
    source: https://github.com/CyBear-Jinni/CBJ_App.git
    flutter-target: lib/main.dart
    override-build: |
      flutter pub run build_runner build --delete-conflicting-outputs
      snapcraftctl build # This line runs the normal `flutter build` and,
        # copies the built file from the normal flutter build/ folder[1]
  1. The build folder(s) that snapcraft will look for are:
    1. build/linux/x64/release/bundle, or,
    2. build/linux/arm64/release/bundle, or,
    3. build/linux/release/bundle
1 Like

Meany thanks @lucyllewy, flutter pub get was needed as well

Here is the final code for anyone needing to run build_runner as well

  cybear-jinni:
    plugin: flutter
    source: https://github.com/CyBear-Jinni/cbj_app.git
    flutter-target: lib/main.dart
    override-build: |
      flutter pub get
      flutter pub run build_runner build --delete-conflicting-outputs
      snapcraftctl build
2 Likes