Custom annotation / Metadata in dart lang

Can anyone explain to me the use of annotations in Dart?

I found this example in the docs:

library todo;

class todo {
  final String who;
  final String what;

  const todo(this.who, this.what);
}

      

followed by

import 'todo.dart';

@todo('seth', 'make this do something')
void doSomething() {
 print('do something');
}

      

So, what should I write in main () to execute the doSomething () function that is being executed?

thank

+3


source to share


1 answer


Something like



import 'dart:mirrors';
import 'do_something.dart';
import 'todo.dart';


void main() {
  currentMirrorSystem().libraries.forEach((uri, lib) {
    //print('lib: ${uri}');
    lib.declarations.forEach((s, decl) {
      //print('decl: ${s}');
      decl.metadata.where((m) => m.reflectee is Todo).forEach((m) {
        var anno = m.reflectee as Todo;
        if(decl is MethodMirror) {
          print('Todo(${anno.who}, ${anno.what})');
          ((decl as MethodMirror).owner as LibraryMirror).invoke(s, []);
        };
      });
    });
  });
}

      

+2


source







All Articles