Automatic disposal
The new context.use API provides fantastic local variables that make it unnecessary to use StatefulWidgets in most (all?) cases.
One potential improvement is automatic detection of a `dispose` method.
Unfortunately in dart, we cannot know ahead of time whether a class has a `dispose` method or not, and interfaces must be explicitly implemented, not simply adhered to. However, it is still possible to access methods that we do not know whether they exist, like this:
```dart
try {
(ourPotentialController as dynamic).dispose();
} on NoSuchMethodError {
// Object does not have a dispose method
}
```
This is also how the `json.encode` from the sdk "detects" `toJson` methods.
With this automatic disposal as a fallback mechanism on omitted `dispose` parameters, it would be possible instantiate controllers at extra sleekness when using `context.use` or `Ref.bind` calls.
1 条评论