Understanding error and warning messages from the Dart analyze

Spread the love

Dart

Have you ever seen an error message.

Let’s take a fairly common example. There’s a problem in the following code:

A simple class with a red squiggly line under the “length” in `values.length`

It seems odd that there’s a problem because we just checked that values isn’t nullon the line above.

The message tells us what’s wrong, but it doesn’t really help us understand why the check wasn’t sufficient or how to respond to the error. That extra information is actually available; it just might not be obvious how to find it.

Although the Dart Analysis view displays only the problem message, double-clicking the message navigates you to the text with the red squiggly underline where the problem is being reported. For other useful features, bring up the context menu:

A screenshot of the context menu for the error message

With the context menu, you can navigate to either the location where the diagnostic was reported (using Jump to Source, which works just like double-clicking the problem message) or to the declaration of values (using the menu item labeled by the context message). Selecting Open Documentation opens the external documentation about the diagnostic.

Visual Studio Code

In Visual Studio Code there are two ways to see the extra information. The first is to expand the entry in the Problems panel:

Screenshot of VS Code’s Problems panel
The third line is a context message that explains why the null comparison wasn’t sufficient to promote the type to be non-nullable. Double-clicking the context message navigates you to the declaration of values. The context message includes the URL of additional documentation related to type promotion.

Summary

I hope this article helps you more easily find the information you need to understand the analyzer’s diagnostics.

If you find diagnostics that are still hard to understand — because the message isn’t clear, it needs a context message to help you find other related code locations, or the documentation isn’t complete enough — please let us know by creating a dart-lang/sdk issue. We’re always interested in improving the tools.

Leave a Reply