Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Fixing Syntax and Type Errors with Dart Analyzer Messages

This lesson is built with resources from youtube.com and dart.dev.

Hello! In the previous lesson, you used if and else to make a program choose between actions. This final lesson in the module is about what to do when a program doesn’t compile: use Dart’s analyzer messages to locate a mistake, decide whether it is a syntax or type error, and check that your fix worked. Allow about 35–40 minutes, including time to make and repair the mistakes yourself.


Ask the analyzer before running

The Dart analyzer examines your source code without executing it. It can flag a missing punctuation mark, a misspelled variable name, or a value used where Dart expects a different type. Your editor may show these problems as underlines while you type; you can also request a report from the terminal.

Using Dart analyze & Dart fix | Decoding Flutter

Watch “Using Dart analyze & Dart fix” from the Flutter channel for a brief explanation of what the analyzer does. For now, focus on analysis rather than the video’s broader discussion of automated fixes.

Watch the analyzer overview. Notice the distinction between editor underlines and running dart analyze yourself: both expose problems found without running your program.

Create or reopen decision.dart in your console-program folder. Start with a working version so you can tell which messages result from each change:

void main() {
  int stock = 2;
  int quantity = 3;

  if (stock < quantity) {
    print('Not enough stock.');
  } else {
    print('Ready to purchase.');
  }
}

In a terminal opened to the folder containing the file, run:

dart analyze decision.dart

You should get a report with no issues. You can also run dart run decision.dart to confirm that this version prints Not enough stock. Analyzing checks the code; running executes it. Use both, because a program can pass analysis yet still make the wrong decision for your intended rules.



Turn a message into a location and a question

An analyzer report usually gives you a severity, a file and position, a description of the problem, and a diagnostic identifier. The position helps you find the code; the description helps you ask, “What did Dart expect here?” Exact wording and layout can vary by Dart version and editor.

The Dart Blog’s Dart diagnostic messages article shows how to inspect a message in an editor or from the command line:

Dart diagnostic messages | The Dart Blog

Read the “Visual Studio Code” and “dart analyze” subsections of this Dart Blog article to learn where problem, correction, and context messages appear. You do not need to understand the article’s nullable-value example yet; concentrate on how to navigate from a message to the relevant code.

In “Visual Studio Code,” begin at “In Visual Studio Code there are two ways to see the extra information.” Read the message guide through the “dart analyze” subsection, including the rest of its sentence about the --verbose flag. Notice how the Problems panel or hover can reveal more detail, and how a message can take you to its reported location.

A useful repair routine is to read the message, inspect the reported line and the line immediately before it, make one small correction, and analyze again. A missing character can cause several later messages, so don’t assume every reported problem needs a separate fix.



Fix a syntax error

A syntax error means Dart cannot interpret the code’s structure as written. Make one deliberately: remove the semicolon after the first print, leaving that part of decision.dart like this:

if (stock < quantity) {
  print('Not enough stock.')
} else {
  print('Ready to purchase.');
}

Run dart analyze decision.dart again. Look for a message such as “Expected to find ';'.” The reported position may be near the next line rather than exactly where the semicolon is missing: Dart discovers the problem when it reaches a token that cannot follow the unfinished statement.

Put the semicolon back after print('Not enough stock.'), then analyze again. The same approach helps with a missing closing parenthesis in if (stock < quantity) { or a missing closing brace: inspect the surrounding structure, not just the character underlined by your editor.



Fix type and name errors

Now keep the punctuation correct, but change the declaration to:

int quantity = '3';

Analyze the file. The message should tell you that a String value cannot be assigned to an int variable. The quotes matter: 3 is an integer literal, while '3' is text. Here, quantity represents a count, so restore the intended value:

int quantity = 3;

A type message is not an invitation to change the declared type automatically. If you changed quantity to String, the later comparison stock < quantity would no longer express a valid numeric comparison. Ask what the value means in this program, then choose a type and value that fit that meaning.

The analyzer also checks conditions. If you replace if (stock < quantity) with if (stock), it will report that the condition must have type bool: stock is an int, not a true-or-false expression. Restore the comparison, or use another Boolean condition appropriate to the decision you want to make.

Finally, a typo such as quantitty in stock < quantitty produces a message about an undefined name. Compare the spelling at the reported location with the earlier declaration of quantity. This is a different problem from a type mismatch: Dart cannot find a variable with the name you wrote.



Check the repair, then check the behavior

After each edit, run dart analyze decision.dart again. If errors remain, start with a clear, early message and inspect nearby code; fixing one cause may remove several messages. Your editor’s hover or Problems panel can provide more detail when a short terminal message is unclear. An error needs attention before you rely on the program; warnings and informational messages may describe other concerns, depending on the checks enabled for the project.

When analysis reports no issues, run:

dart run decision.dart

With stock set to 2 and quantity set to 3, the program should print Not enough stock. A clean analyzer report means the code satisfies the checks it performs—not that your purchasing rule is correct. For example, changing < to > might pass analysis while giving the wrong answer for a shortage.



Key takeaways

Use dart analyze decision.dart or your editor’s diagnostics to find a reported problem without running the program. Read the location and message together, inspect nearby lines, make one targeted fix, and analyze again. Syntax errors concern code structure; type errors concern whether a value fits where it is used. Once analysis is clean, run the program to check its behavior too.

You’ve finished the foundations for writing and troubleshooting small Dart console programs. In the next module, you’ll work with collections, beginning with a typed List for storing multiple values.