Given the following Dart/Flutter class structure:
import 'package:flutter/material.dart';
class A with ChangeNotifier {
B _element1;
B _element2;
B get element1 => _element1;
B get element2 => _element2;
set element1(B value) {
_element1 = value;
notifyListeners();
}
set element2(B value) {
_element2 = value;
notifyListeners();
}
}
class B {
String x;
String y;
}
I am trying to listen to a change of A.element1.x
but the problem is, the setter of class B can't call the notifyListeners()
of the class A, so either I am Listening to A and won't notice a change or I am listening to B and I am losing the context to A.
I am using the Provider package in my Flutter project. But I am not sure, if I am misunderstanding the concept of Provider package or ChangeListeners. Either way I am not able to find a elegant solution.
Is there a possibility to overwrite the setter of class B from class A? I could obviously implement a function for each element1 and element2 fields(x,y). But this is not good code style I guess.