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

? Android (apk,appbundle) ➡ How to build for Android
? iOS (ios,ipa) ➡ How to build for iOS
? Web (web) ➡ How to build for web
? Linux (linux) ➡ How to build for Linux
? Windows (windows) ➡ How to build for Windows
? Mac OS (macos) ➡ How to build for Mac OS

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.