How To Use The Flutter Command-Line Interface

Flutter category image

In this article, we are looking at the Flutter CLI and its most common use cases for building, testing, deploying, and running Flutter applications.

When developing Flutter apps with code editors like Visual Studio Code, Android Studio, or XCode, you donโ€™t need to use the command-line tools of Flutter in general. But all these editors use them internally all the time, probably without you noticing it. So letโ€™s dive into the Flutter CLI and see what its capabilities are.

General

โ–ถ flutter doctor [-v]

The doctor tells you the current state of your system and if there is anything missing to work with Flutter. It shows SDK versions, finds code editors, lists connected devices, and identifies problems. Use the -v switch to get more detailed information.

Output of the Flutter doctor command
Output of the Flutter doctor command

โ–ถ flutter create

With this command, you can create a new Flutter project or repair an existing one. It allows you to specify a project name (--project-name), a description (--description), supported platforms like iOS, Android, Windows, Linux, macOS, and web (all are enabled by default), or what type of project it is (--template).

Output of Flutter create command
Output of Flutter create command

There are other ways to create a new Flutter project besides the CLI. Check my article below to find out how.

โ–ถ flutter channel

Use this to switch between the different development channels. There are currently four channels available: stable, beta, dev, and master. Stable is the safest channel with the most testing done, master contains hot new stuff and can change daily.

Output of Flutter channel command
Output of Flutter channel command

โ–ถ flutter upgrade

Upgrades to another version on the current channel. With the switch
--verify-only you can check for new versions without upgrading.

Output of Flutter upgrade command
Output of Flutter upgrade command

Implementation

โ–ถ flutter pub [add/outdated/upgrade/get]

With this command, you can manage your packages in your Flutter app. It helps you add packages with add, find outdated versions with outdated, upgrade them with upgrade, or synchronize them with get.

Output of Flutter pub outdated command
Output of Flutter pub outdated command

โ–ถ flutter build [target]

The build command creates a deployable app from your code to the target platform. You need to provide a subcommand to indicate what target you want to build. Your options are

Only available options are shown in the help menu (flutter build -h) depending on your current system. That means if you are, for example, developing on a Windows machine, you wonโ€™t have the option to build for Mac OS.

Output of Flutter build command
Output of Flutter build command

โ–ถ flutter clean

This command cleans caches and deletes build output data. If your app isnโ€™t working as expected or throwing suspicious errors, try this command and run it again.

Output of Flutter clean command
Output of Flutter clean command

Deployment

โ–ถ flutter test

Runs unit tests of the current project. This command has many options available. You can measure code coverage with the switch --coverage for example.

Output of Flutter test command
Output of Flutter test command

If you want to know more about testing with Flutter, check these two articles about testing and code coverage.

โ–ถ flutter devices

List all available and connected devices like mobiles, browsers, or operating systems.

Output of Flutter devices command
Output of Flutter devices command

โ–ถ flutter install

Installs the app to a device. If multiple devices are available, the command will ask for the device.

Output of Flutter install command

โ–ถ flutter run [arguments]

Runs the current app on the selected device. Here are some frequently used arguments:

  • --debugย runs in debug mode
  • --profileย runs in profile mode for optimized performance measuring
  • --releaseย runs in release mode
  • --trace-startupย trace app startup and save file into build directory
  • --routeย define a custom route to load on app launch
  • --flavorย build a custom flavor/scheme depending on the platform
Output of Flutter run command
Output of Flutter run command

Conclusion

The Flutter CLI is very powerful and offers some very good and useful tools. But there might be a chance that you donโ€™t ever need to run a single command yourself because your IDE does all the work for you. But if you ever need to use the CLI, those insights above might be quite handy.