addStrongRefListener method

void addStrongRefListener({
  1. required VoidCallback strongRefListener,
})

Register a closure to be called when the object notifies its listeners.

The listener must be strongly referenced, meaning it should be stored in an instance variable or field. If not, it will be garbage collected prematurely.

For example:


// 👍 CORRECT - Instance functions are strongly referenced:

final listener = () {
  print('Pod value changed');
};
weakChangeNotifier.addStrongRefListener(strongRefListener: listener);

// ❌ INCORRECT - Functions defined like this are not strongly referenced:

void listener() {
  print('Pod value changed');
}

weakChangeNotifier.addStrongRefListener(strongRefListener: listener);

// ❌ INCORRECT - Anonymous functions are not strongly referenced:

weakChangeNotifier.addStrongRefListener(strongRefListener: () {
 print('Pod value changed');
});

Implementation

void addStrongRefListener({
  required VoidCallback strongRefListener,
}) {
  // ignore: deprecated_member_use_from_same_package
  addListener(strongRefListener);
}