我正在尝试为 Decimal 类编写扩展方法:
void parse() {
Decimal two = Decimal.two;
}
extension on Decimal {
static Decimal get two => Decimal.fromInt(2);
}
然而,Dart 说“没有为类 Decimal 定义 getter 'two'。”扩展方法是否仅适用于 Dart 原生类?
谢谢!
最佳答案
在您的导入中使用以下代码行:
import 'YourFileName.dart' show ExtensionName;
在你的“YourFileName.dart”中应该是这样的:
extension ExtensionName on Decimal {
static Decimal get two => Decimal.fromInt(2);
}
然后根据您的要求使用它。
让我知道您是否仍然使用 卡在任何地方扩展 与 Dart .
关于dart - 我可以在 Dart 中的任何类上使用扩展方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59433532/