2

Please check the two samples below.

  • The first sample does not rebuild the Widgets [Possibly 'listeners' are not being 'notified']
  • The second sample works as expected

To my understanding, i think all these two should work. Can someone brief me on the comprehension I'm lacking? Thanks in advance.



Sample one (Does not rebuild) [ui changes do not take effect ]

onTap: (String? newValue) {
ref.watch(UserProvider).selectedMaritalStatusValue = newValue!;
UserModel().notifyAllListeners(); //triggers notifyListeners
 },

Sample Two (Does rebuild)[working fine]

onTap: (String? newValue) {
ref.watch(UserProvider).setMaritalStatus(newValue!); // 'setMaritalStatus' has notifyListeners trigger within
 },
ket-c
  • 211
  • 1
  • 10
  • What type of provider are you using? Can you also add the code for the provider? – Josteve Dec 13 '22 at 01:48
  • ChangeNotifierProvider – ket-c Dec 13 '22 at 01:50
  • It's **StateNotifierProvider** rather ```final UserProvider = StateNotifierProvider((ref) { return UserModel(); });``` – ket-c Dec 13 '22 at 07:35
  • **UserModel** ```class UserModel extends ChangeNotifier { String selectedGenderValue = 'Females'; notifyAllListeners() { notifyListeners(); }``` – ket-c Dec 13 '22 at 10:33

2 Answers2

0

You can use a private variable setter, then call notifyListeners in the setter after updating the variable like so:

class UserProvider extends ChangeNotifierProvider{
    String? _selectedMaritalStatusValue;
    String? get selectedMaritalStatusValue => _selectedMaritalStatusValue;
    set selectedMaritalStatusValue(String? newValue){
      _selectedMaritalStatusValue = newValue;
      notifyListeners();
    }
}

Now, this should work:

ref.watch(UserProvider).selectedMaritalStatusValue = newValue!;
Josteve
  • 11,459
  • 1
  • 23
  • 35
  • Sorry. I'm using **StateNotifierProvider** instead I'll edit my question with the full code if necessary so that you'll get what issues well, I think there's a lil confusion for me here. – ket-c Dec 13 '22 at 07:33
  • Hey @ket-c, please add the code for the UserModel. – Josteve Dec 13 '22 at 09:37
  • ```class UserModel extends ChangeNotifier { notifyAllListeners() { notifyListeners(); }``` – ket-c Dec 13 '22 at 10:28
0

Firstly, you should not use ref.watch inside any onTap callbacks. Use ref.read here instead. Read this for clarity on why this is the case.

Secondly, in your first code block where you write:

UserModel().notifyAllListeners();

UserModel() creates a new object altogether, and notifyAllListeners() is being called for this new object. This new object is not being watched inside the build method of this widget. This is why the first code block you posted fails to rebuild the widget.

Thirdly, as a best practice, methods like notifyListeners() and direct assignments of fields in any class should be done inside the class's code. Use your second code block as a reference in future. This is the correct and safest way.

som-R91
  • 516
  • 2
  • 7