在DART 2.6中,我可以使用如下扩展方法:
extension on int {
int giveMeFive() {
return 5;
}
}
main(List<String> arguments) async {
int x;
x.giveMeFive();
}
一切运行完美!:D
但是现在如果我想把我的扩展名方法放到另一个文件中,像这样:
main.dart
import 'package:untitled5/Classes/ExtendInt.dart';
main(List<String> arguments) async {
int x;
x.giveMeFive();
}
ExtendInt.dart个
extension on int {
int giveMeFive() {return 5;}
}
它失败了,并出现令人沮丧的错误.
bin/main.dart:9:5: Error: The method 'giveMeFive' isn't defined for the class 'int'.
Try correcting the name to the name of an existing method, or defining a method named 'giveMeFive'.
x.giveMeFive();
^^^^^^^^^^
是不允许这样做还是我做错了什么? 感谢阅读!:)