Is it possible to get the class type in a static method?
dart, types
Solution
Since `onmystatic()` is static, any functions it calls must static (or a instance method of some object), so I'll assume `printCurrentType()` is also static. Since static methods can't be overridden, the type is constant and you can just write:
class User {
static onmystatic() {
printCurrentType();
}
static printCurrentType() {
print(User);
}
}
If you wanted `printCurrentType()` to be some generic method that printed the type of the containing class of any static method it was called from, well... that's a much tougher task. The easiest answer is just don't try to do that and pass the class as a parameter:
class User {
static onmystatic() {
printCurrentType(User);
}
}
printCurrentType(Type type) {
print(type);
}
The complicated answer is that you could throw an exception, parse the stack trace, and try to determine by some rules which class you should print. I'll leave that as an exercise to the reader :)
Problem
Dart code: ``` class User() { static onmystatic() { printCurrentType(); // should print: User } } ``` Notice the `printCurrentType()` in the class `User`, is it possible to implement it? I tried `this.runtimeType` but it reminds me `this` is not in scope.