'Listen a ValueNotifier from class

Hi im new in flutter and i have a class for notify changes of a global values in my app like this

import 'package:flutter/material.dart';


class GlobalNotifier {

  final counter = ValueNotifier<int?>(null);

  setValue(int value){
    counter.value = value;
  }

}

In another page i set the value with tap event executing this

var globalNotifier = GlobalNotifier();
globalNotifier.setValue(widget.item['id']);

I tested that the value is really changed and test the listener in the class GlobalNotifier its working fine but when i add a listener out of the class GlobalNotifier not notify nothing

var model = GlobalNotifier();
print('charged listeners');
model.counter.addListener(() {
  print('counter changed!');
  print(model.counter.value);
});

Thanks!



Solution 1:[1]

var model = GlobalNotifier();

print('charged listeners');

model.counter.addListener(() {
    print('counter changed!');
    print(model.counter.value);
});

model.setValue(0 or [youvalue]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HOZERO