Using annotations

Dart shares with other languages such as Java and C# the ability to attach (or annotate) variables, classes, functions, methods, and other Dart program structures with metadata words preceded by an @ sign. This is done to give more information about the structure, or indicate that it has a special characteristic or behavior. Examples are @override, @deprecated, and @observable (used in Polymer), so they are liberally used by the Dart team. Also, Angular.dart uses them abundantly. Moreover, you can also define your own annotations.

How to do it...

In the project annotations, we gave our Embrace class the metadata @ToFix. The strangle method is denoted by @deprecated, and we indicate with @override in Embrace that we want to override the method consumedCalories inherited from Movement, as shown in the following code:

const Anno = "Meta";

void main() {
  var embr = new Embrace(5);
  print(embr);
  var str = new Embrace. strangle();
}

@Anno
@ToFix("Improve the algorithms", "Bill Gates")
class Embrace {
// code ommitted, see previous recipes
@deprecated
  Embrace.strangle(): _strength =
 100;

@ov
erride consumedCalories() { }  //      warning!

}

class Movement {
  String Name;
  Movement();
  consumedCalories() {
    // calculation of calories
  }
}

class ToFix {
  final String note, author, date;
  const ToFix(this.note, this.author, {this.date});
}

How it works...

The @deprecated instance is used to indicate something that you no longer want users of your library to use, and that will probably stop working in a future version. When the analyzer in Dart Editor sees this annotation, it marks the code component that follows (and everywhere it is used) with a strike-through line. Moreover, it will give a warning: '…' is deprecated. The @override instance is also a good (but not necessary) indication that you want to override an inherited behavior. Here, the editor also uses this instance to point to possible bugs; if you had written @override consumedcalories(), then you would get the warning Method does not override an inherited method; so typos are eliminated.

To make your own annotations, you must make sure that it is defined as a constant expression that starts with an identifier, such as const Anno in the example, which could be used as @Anno. More specifically, it must be a reference to a compile-time constant variable or call to a constant constructor. We used the class ToFix to give our class the annotation @ToFix("Improve the algorithms").

There's more...

Annotations are defined in Dart through the class Annotation in the analyzer library. With the reflection mirror library (see the Using reflection recipe), it is possible to extract metadata at runtime and use its values to influence program execution.

See also

  • See the annotations used in Angular.dart in Chapter 11, Angular Dart Recipes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset