I wonder if it is possible to write an extension function that will cast dynamic to Class of my choice, so that I may have fields suggestions in my IDE.
I've tried somethings like this:
extension DynamicCasting on dynamic {
T as<T>() => this is T ? this as T : null;
}
but when I used it
class User {
final String name;
User(this.name);
}
...
foo[i].as<User>().
It still sees it as dynamic and name field isn't suggested.
It seems to work in dartpad I can see the expected member of the String class used in .as<String>().
with the following snippet. So it looks like an issue in your IDE and/or your project setup.
void main() {
print(1.as<String>().);
}
extension DynamicCasting on dynamic {
T as<T>() => this is T ? this as T : null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With