1

I have a Widget containing a button which increases product quantity in a product list and shows the updated value in a textBox. I am using Riverpod for Statemanagement.

Here is a ItemCounter Widget:

final appNotifier = ref.watch(applicationProvider.notifier);

ItemCounter(
            quantity: appNotifier.getQuantityByProductId(product.id!),
            onChanged: (i) => appNotifier.setProductQuantity(
                i: i, productId: product.id!),
          ), 

When I use above code the UI doesn't update after each click on the button! But when I add this line it works, even though I don't use appProvider anywhere in the code!

// appProvider has no usage in the code but it helps to update UI
final appProvider = ref.watch(applicationProvider);                 <-----This line 
final appNotifier = ref.watch(applicationProvider.notifier);

ItemCounter(
            quantity: appNotifier.getQuantityByProductId(product.id!),
            onChanged: (i) => appNotifier.setProductQuantity(
                i: i, productId: product.id!),
          ), 

1 Answer 1

6

By design :)

-> When we use ref.watch(applicationProvider), we listen for state changes (and access it) and trigger a rebuild of the widget whenever it has changed.

-> When we use ref.watch(applicationProvider.notifier), we access the listener class and always have the current version of notifier. The rebuilding will not be triggered.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.