4 Dart Commands Every Flutter Dev Should Know

Temidayo Adefioye
4 min readSep 5, 2022
Image Credit: Swan Software Solutions

So a senior engineer was questioned by a tech novice. Why do Java programmers use eyeglasses?
They are unable to C#, so he responded.

Seriously, what the heck is dart?

Tenor

The Dart team is regularly introducing new features that would help developers create apps more quickly while also increasing work productivity. With the dart command line tool, you can utilize the built-in capabilities of the dart language quickly.

The dart tool (bin/dart) is a command-line interface to the Dart SDK. Whether you download the Dart SDK explicitly or just the Flutter SDK, the utility is accessible regardless of how you obtain the Dart SDK.

Let’s look at four commands that any Flutter/Dart developer has to know.

Dart Analyze

The static analysis carried out by the dart analyze command is identical to that performed by an IDE or editor that supports Dart.

Here is an illustration of static analysis applied to all Dart files in the current directory:

Dart Create

Using one of the provided templates, the dart create command generates a new Dart project. IDEs have the same features accessible.

A directory containing the project files is first created when you run dart create. Then, package dependencies are added (unless you specify the — no-pub flag).

Here’s an example of using dart create to create a directory named my_cli that contains a simple console app (the default template):

dart create my_cli

Add a template argument to use an alternative template, such as web-simple:

dart create -t web-simple my_web_app

Dart Fix

The dart fix command detects and resolves two categories of problems:

  • Analysis issues identified by dart analyze that have associated automated fixes (sometimes called quick-fixes or code actions)
  • Outdated API usages when upgrading to more recent versions of the Dart and Flutter SDKs.

Use the — dry-run flag to preview proposed changes:

$ dart fix --dry-run

Use the — apply flag to put the suggested changes into effect.

$ dart fix --apply

Customizing dart fix

The dart fix command only implements fixes when a diagnostic identifies a “problem” as existing. Since everyone has different preferences, some diagnostics, like compilation errors, are implicitly enabled, whilst others, like lints, require specific activation in the analysis options file.

By enabling more lints, you may occasionally be able to enhance the number of fixes that can be performed. Keep in mind that not all diagnostics have corresponding fixes.

Example

Imagine you have code like this:

Configure your analysis_options.yaml file as follows to enable dart fix to upgrade old code to make advantage of this capability and to make sure that the analyzer alerts you if you subsequently neglect to utilize it:

linter:
rules:
- use_super_parameters

Additionally, we must ensure that the code supports the necessary language version. Update pubspec.yaml to include super initializers at the very least in the lowest SDK constraint since super initializers were introduced in Dart 2.17:

environment:
sdk: ">=2.17.0 <3.0.0"

Dart Format

To insert formatting that adheres to Dart rules in place of whitespace in your program, use the dart format command. The formatting is the same as what you would see in a Dart-compatible IDE or editor.

Visit the Dart command-line tool page for more details on this and other dart commands.

Give the dart format command a list of files or directories to work with. To format all the Dart files in or under the current directory, for instance, follow these steps:

$ dart format .

Use a space-delimited list to specify multiple files or directories. All of the Dart files in the lib directory and one Dart file in the bin directory are formatted by the following command:

$ dart format lib bin/updater.dart

Dart format overwrites the Dart files by default.

Add the — output or -o flag if you don’t want to overwrite the files. Use the options -o show or -o json to get the formatted files’ contents, or -o none to see only the files that would change.

$ dart format -o show bin/my_app.dart

The — set-exit-if-changed flag tells the command to exit with a code of 1 if the formatting changes. When using continuous integration (CI), this exit code is frequently used to denote that a check should fail.

Remember to leave your feedback and give this post as many claps as you can if you liked it. 🤗

--

--

Temidayo Adefioye
Temidayo Adefioye

Written by Temidayo Adefioye

Founder, CodeNest Africa | [in]structor | Software Engineer | Speaker | Author

No responses yet