dartoverridingdart-mirrorspetitparser

@override of Dart code


I noticed PetitParserDart has a lot of @override in the code, but I don't know how do they be checked?

I tried IDEA dart-plugin for @override, but it has no effect at all. How can we use @override with Dart?


Solution

  • The @override annotation is an example of metadata. You can use Mirrors to check for these in code. Here is a simple example that checks if the m1() method in the child class has the @override annotation:

    import 'package:meta/meta.dart';
    import 'dart:mirrors';
    
    class A {
      m1() {}
    }
    
    class B extends A {
      @override m1() {}
    }
    
    void main() {
      ClassMirror classMirror = reflectClass(B);
      MethodMirror methodMirror = classMirror.methods[const Symbol('m1')];
      InstanceMirror instanceMirror = methodMirror.metadata.first;
      print(instanceMirror.reflectee);  // Instance of '_Override@0x2fa0dc31'
    }