Riverpod 中的 autoDispose 修改器
我在提供程序中有多个值(在类中),当我在提供程序中使用 autoDispose 修饰符时,我只想处理提供程序中的几个值,是否可以这样做?
我在互联网上搜索过此内容,但没有找到与此要求相关的任何资源。
final provider = StateProvider((ref) => LoginDetails(
errorMessage: '', status: 0));
class LoginDetails {
int status;
String errorMessage;
LoginDetails({
required this.status,
required this.errorMessage,
});
LoginDetails copyWith({
int? status,
String? errorMessage,
}) =>
LoginDetails(
status: status ?? this.status,
errorMessage: errorMessage ?? this.errorMessage,
);
}
I have multiple values (in class) in provider and when I use the autoDispose
modifier in provider then I want to dispose of only a few values in the provider is it possible to do like this?
I have searched for this on the internet but I didn't find any resources related to this requirement.
final provider = StateProvider((ref) => LoginDetails(
errorMessage: '', status: 0));
class LoginDetails {
int status;
String errorMessage;
LoginDetails({
required this.status,
required this.errorMessage,
});
LoginDetails copyWith({
int? status,
String? errorMessage,
}) =>
LoginDetails(
status: status ?? this.status,
errorMessage: errorMessage ?? this.errorMessage,
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有办法让
autoDispose
只处理一些属性。autoDispose
要么全有,要么全无另一方面,使用 2.0.0-dev 版本,您可以使用
ref.onCancel
在最后一个侦听器被删除时执行一些逻辑 - 这即使在非 autoDispose 的提供程序上也可用,因此您可以执行以下操作:
There is no way to have
autoDispose
only dispose a few property.autoDispose
is all or nothingOn the other hand, using version 2.0.0-dev, you can use
ref.onCancel
to perform some logic when the last listener is removed – which is available even on providers that are notautoDispose
So you can do:
我知道有两种方法可以做到这一点
第一种方法是将变量设为私有静态
第二种方法是使用另一个提供程序来包含您不想丢失的所需信息,我更喜欢这种方式。
我仍然确信有很多替代解决方案,但这完全取决于您的用例。
I know two ways to do that
The first way is to make the variables private static
The second way is to have another provider that contains the needed information which you don't want to lose, and I prefer this way.
and still I am sure there is many alternative solutions out there but it's all depends on your use case.