Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provider.disposable() Feature Request #108

Closed
DenisBogatirov opened this issue Jun 3, 2019 · 8 comments
Closed

Provider.disposable() Feature Request #108

DenisBogatirov opened this issue Jun 3, 2019 · 8 comments
Labels
enhancement New feature or request

Comments

@DenisBogatirov
Copy link

DenisBogatirov commented Jun 3, 2019

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.

@rrousselGit
Copy link
Owner

Out of curiosity, why does your example takes a T value instead of a T Function(BuildContext) builder?

@rrousselGit
Copy link
Owner

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.

@DenisBogatirov
Copy link
Author

DenisBogatirov commented Jun 3, 2019

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

@rrousselGit
Copy link
Owner

rrousselGit commented Jun 4, 2019

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

@DenisBogatirov
Copy link
Author

I am already using my custom provider. But I'm forced to copy this Provider on each of my projects.

@jogboms
Copy link
Contributor

jogboms commented Jun 5, 2019

@DenisBogatirov This is why,

"Named constructors cannot have different generic constraints from the other constructors."

@rrousselGit
Copy link
Owner

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 👍 the issue.

@rrousselGit
Copy link
Owner

rrousselGit commented Apr 11, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants