Dart18.元数据(注解)-@deprecated

薛烨霖
2023-12-01

所有的 Dart 代码都可以使用: @deprecated 和 @override。

mata1.dart

import 'todo.dart';

main() {
  dynamic tv = new Television();
  tv.activate();
  tv.turnOn();
  tv.turnOff();
  tv.doSomething();
}

class Television {
  @deprecated
  void activate() {
    turnOn();
  }

  void turnOn() {
    print('Television turn on!');
  }

  @override
  noSuchMethod(Invocation mirror) {
    print('没有找到方法');
  }

  @Todo(who: 'damon', what: 'create a new method')
  void doSomething() {
    print('doSomething');
  }
}

toto.dart

class Todo {
  final String who;
  final String what;

  const Todo({this.who, this.what});
}

输出:

D:\flutter\bin\cache\dart-sdk\bin\dart.exe --enable-asserts --enable-vm-service:48868 D:\Code\Flutter\FlutterHello\flutter_app\lib\meta1.dart
lib/meta1.dart: Warning: Interpreting this as package URI, 'package:flutterapp/meta1.dart'.
Observatory listening on http://127.0.0.1:48869/uKs7GjFjfhI=/

Television turn on!
Television turn on!
没有找到方法
doSomething

Process finished with exit code 0

 

 类似资料: