autoDispose modifier in riverpod

525

There is no way to have autoDispose only dispose a few property. autoDispose is all or nothing

On 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 not autoDispose

So you can do:

final provider = Provider((ref) {
  ref.onCancel(() {
    // TODO reset the few properties of your choice
  });

  return SomeState();
});
Share:
525
Admin
Author by

Admin

Updated on January 03, 2023

Comments

  • Admin
    Admin over 1 year

    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,
          );
    }
    
    • Yeasin Sheikh
      Yeasin Sheikh about 2 years
    • Admin
      Admin about 2 years
      @YeasinSheikh i have added code.... i want to dispose only errorMessage in the LoginDetails
  • Admin
    Admin about 2 years
    // TODO reset the few properties of your choice can you please explain it?