Repository: rrousselGit/state_notifier

Issue:
A few state notifiers stopped working, is it due to updateShouldNotify?
I just did a `flutter pub upgrade` a few hours ago and the only thing I noticed changed was this package.

At the same time a few (not all) of my state notifiers stopped working as expected and reading through I just noticed this was changed https://github.com/rrousselGit/state_notifier/pull/57

Wondering if this is related or if there is any quick statement on how this `updateShouldNotify` works?

My code that stopped working is:

```
final paginationProvider = StateProvider.family((ref, id) => true);
```

```
  return Container(
          child: Consumer(
          builder: (context, watch, _)
                {

                  _showPaginationBar = ref.watch(paginationProvider(widget.randomHash).state).state;
```

This provider is updated in another class, when the screen is scrolled as follows:

```
    if ((_thisScrollDirection == 'down') && (metrics.pixels > _lastTappedScrollPosition+100.0))
    {
      ref.read(paginationProvider(_randomHash).state).state = false;
    }
```

It doesn't seem to update anymore, maybe I don't need Consumer anymore since it is inside a ConsumerStatefulWidget ?  This is some of my most early/old code in my app, but I am unsure if this is the reason?  

It all seemed to be working smoothly just before this update

Thanks!



Discussion: Could you try adding to your pubspec.yaml:

```dart
dependencies:
  state_notifier: 0.7.1
```

to see if the issue disappears?
> Could you try adding to your pubspec.yaml:
> 
> ```dart
> dependencies:
>   state_notifier: 0.7.1
> ```
> 
> to see if the issue disappears?

Yup, all fixed now, problem is gone 
Id rather stay on the latest versions, do you think this an issue of my poor coding that I could try to fix or a bug and I should wait for an update?

Thanks much
I'm a huge fan of this package and riverpod. I'm having the exact same issue. From what I can tell it's a regression in 0.7.2: It's [setting `_state` to the new value *before* calling `updateShouldNotify`](https://github.com/rrousselGit/state_notifier/commit/9c878df5fc5a80831b7f4dde2f7370c36dd7235e#diff-af6db22299ddfd4adcf615d58f983cb787848d97e8c1612732402fc281e3a24cR212):
```dart
@protected
  set state(T value) {
    assert(_debugIsMounted(), '');
    if (identical(_state, value)) {
    final previousState = _state;
    _state = value;

    /// only notify listeners when should
    if (!updateShouldNotify(previousState, value)) {
      return;
    }
```

But then the base implementation of `updateShouldNotify` compares `_state` vs `current`, which will always be true! (_state was already set to the new value before `updateShouldNotify` is called).
```
  /// Whether to notify listeners or not when [state] changes
  @protected
  bool updateShouldNotify(
    T old,
    T current,
  ) =>
      !identical(_state, current);
```

Instead, it needs to compare old vs current. Ideally a test case could be added to ensure that a change notification is triggered when state value changes.

Fix patch:
diff --git a/packages/state_notifier/lib/state_notifier.dart b/packages/state_notifier/lib/state_notifier.dart
index 613fd3d..c634e83 100644
--- a/packages/state_notifier/lib/state_notifier.dart
+++ b/packages/state_notifier/lib/state_notifier.dart
@@ -203,7 +203,7 @@ Consider checking `mounted`.
     T old,
     T current,
   ) =>
-      !identical(_state, current);
+      !identical(old, current);
 
   @protected
   set state(T value) {


Test patch:
diff --git a/packages/state_notifier/test/update_shoud_notifiy_test.dart b/packages/state_notifier/test/update_shoud_notifiy_test.dart
index 844be17..216ef18 100644
--- a/packages/state_notifier/test/update_shoud_notifiy_test.dart
+++ b/packages/state_notifier/test/update_shoud_notifiy_test.dart
@@ -23,6 +23,12 @@ class TestNotifier extends StateNotifier<int> with Mock {
   }
 }
 
+class FakeTestNotifier extends StateNotifier<int> {
+  FakeTestNotifier() : super(0);
+
+  void increment() => state++;
+}
+
 void main() {
   test(
     'it updates and does not notify when updateShouldNotify return false',
@@ -45,4 +51,23 @@ void main() {
       expect(notifier.debugState, 0);
     },
   );
+
+  test(
+    'update should notify receive two different refs',
+    () {
+      // will notify if not identical
+      final notifier = FakeTestNotifier();
+      final listener = Listener();
+
+      notifier.addListener(listener);
+
+      verify(listener(0));
+      verifyNoMoreInteractions(listener);
+
+      notifier.increment();
+
+      verify(listener(1));
+      verifyNoMoreInteractions(listener);
+    },
+  );
 }