Sdk: Analyzer error for annotations that aren't in scope is confusing

Created on 8 Nov 2016  路  11Comments  路  Source: dart-lang/sdk

This code:

final arglbargl2 = null;

void main () {
  @arglbargl List<dynamic> foo;
  @arglbargl2 List<dynamic> bar;
  arglbargl baz;
  arglbarl.toString();
}

Produces the following error messages:

[error] Annotation must be either a const variable reference or const constructor invocation. (/Users/leafp/tmp/test.dart, line 4, col 3)
[error] Annotation must be either a const variable reference or const constructor invocation. (/Users/leafp/tmp/test.dart, line 5, col 3)
[error] Undefined class 'arglbargl'. (/Users/leafp/tmp/test.dart, line 6, col 3)
[error] Undefined name 'arglbarl'. (/Users/leafp/tmp/test.dart, line 7, col 3)

Note that the last two make it very clear that something is not defined.

The first error message though is confusing, since it suggests that the problem is that the name used in the annotation might just not be const. The second error message illustrates this: in that case, the name is in scope but is not const, but the error message is identical to the first case in which the name was not in scope.

P2 analyzer-ux area-analyzer customer-flutter type-bug

Most helpful comment

This just hit me. I was writing a class and wanted to make use of required parameters in a constructor. I see this all over the flutter standard library so it makes sense. I even double checked how flutter was doing it in its own source code. But no matter what I did I kept getting.

"Annotation must be either a const variable reference or const constructor invocation."

After a while, and also finding this github issue, I finally figured out that I needed to include

import 'package:flutter/foundation.dart';

In order to use the @required annotation.

All 11 comments

Any updates? Would just changing the error string improve it - I still see people struggling with it, especially beginners.

I think we should split this into two different errors: one for when the name is not defined and another for when the name is defined as something other than a constant.

This just came up from a Flutter user's feedback. Is this an easy fix? Hopefully it's low-hanging fruit. Thanks!

cc @lukechurch who is cataloging these

This just hit me. I was writing a class and wanted to make use of required parameters in a constructor. I see this all over the flutter standard library so it makes sense. I even double checked how flutter was doing it in its own source code. But no matter what I did I kept getting.

"Annotation must be either a const variable reference or const constructor invocation."

After a while, and also finding this github issue, I finally figured out that I needed to include

import 'package:flutter/foundation.dart';

In order to use the @required annotation.

This bit me yesterday on Dartdoc. A bug in change I was working on resulted in the analyzer not having all the libraries it needed loaded to generate documentation (roughly the same problem as not having an import), and this analysis warning was my only direct clue to what was wrong.

@dragonfax thanks for the info!! Can this be put into the error message @rkj?

This got me too - and this is the only page on google to help you work out what the error is supposed to mean.

+1. Do what @dragonfax says.

I think this error is increasingly common now given the style that Flutter uses Dart.

@tvolkert reports that this is being seen more and more - adding to Dart2Beta4 milestone.

As of https://dart-review.googlesource.com/c/sdk/+/52240, the first error now reads:

error: Undefined name 'arglbargl' used as an annotation. (undefined_annotation at [test_package] lib/test.dart:4)

The rest are unchanged, but they seem fairly reasonable to me. Happy to iterate if this is still causing issues for people.

I had to revert the change because I had trouble figuring out how to update the status files to get the bots green. I have since figured it out and re-landed (https://dart-review.googlesource.com/c/sdk/+/52640).

Was this page helpful?
0 / 5 - 0 ratings