It would be greate to have Provider.disposable<T> factory, where T implements Disposable
cabstract class Disposable {
void dispose();
}
factory Provider.disposable<T extends Disposable>({ Key key, @required T value, Widget child, } ) {
assert(value != null);
return Provider<T>(
key: key,
builder: (_) => value,
dispose: (_, v) => v.dispose,
child: child,
);
}
It is very usefull in pair with BLoC pattern, you just override dispose method for each bloc and then use.
Out of curiosity, why does your example takes a T value instead of a T Function(BuildContext) builder?
Note that this is not possible to do on Provider. Named constructors cannot have different generic constraints from the other constructors.
So we can't do factory Provider.disposable<T extends Disposable>
The only way would be to have a DisposableProvider instead.
It takes T value because of my way of using it. I'm doing something like this
initState() {
_bloc = Bloc();
}
build() {
...
MultiProvider(
provedrs: [
DisposableProvider (value: _bloc),
...
],
...
}
T (BuildContext) builder will work form me as well as DisposableProvider
I'm mixed about that.
Since there's no implicit interface implementation in Dart, our Disposable interface wouldn't be compatible with most disposable objects.
Considering the simplicity of the provider, I'd recommend making your own by subclassing Provider
I am already using my custom provider. But I'm forced to copy this Provider on each of my projects.
@DenisBogatirov This is why,
"Named constructors cannot have different generic constraints from the other constructors."
An alternative is a static method:
class Provider<T> {
static disposable<T extends Disposable>({ValueBuilder<T> builder}) {
return Provider(builder: builder, dispose: (_, d) => d.dispose());
}
}
If that feature is truly desired, I encourage peoples to :+1: the issue.
Unless the official Dart sdk includes a Disposable interface, I don't think I will include such feature.
An alternative would be for dart to add static extension methods, or for you to make a custom DisposableProvider
Most helpful comment
An alternative is a static method:
If that feature is truly desired, I encourage peoples to :+1: the issue.