Listen a ValueNotifier from class

Viewed 1224

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!

1 Answers
var model = GlobalNotifier();

print('charged listeners');

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

model.setValue(0 or [youvalue]);
Related