I have two controllers using the changenotifiers in my app and I want to notify listeners of A from B. I have be trying two different methods for the same.
Method 1
(Controller A):
class DrawController extends ChangeNotifier {
void clearCanvas() {
selectedText = '';
shouldClearCanvas = true;
currentDrawPoints.initialPoint = null;
currentDrawPoints.endPoint = null;
print(currentDrawPoints.initialPoint);
notifyListeners();
}
}
Calling it from (Controller B):
class GameController extends ChangeNotifier {
DrawController drawController = DrawController();
void someFunction(){
drawController.clearCanvas();
notifyListeners();
}
}
The above method doesn't work. It does execute the function and changes the value but it does not change the UI (does not rebuild the widget).
Method 2:
(Controller A):
class DrawController extends ChangeNotifier {
void clearCanvas() {
selectedText = '';
shouldClearCanvas = true;
currentDrawPoints.initialPoint = null;
currentDrawPoints.endPoint = null;
print(currentDrawPoints.initialPoint);
notifyListeners();
}
}
Calling it from (Controller B):
class GameController extends ChangeNotifier {
DrawController drawController = DrawController();
void someFunction(BuildContext context){
Provider.of(context, listen: false).clearCanvas();
}
}
This second method works and it updates the UI too but I think this is the wrong method of doing it.
As using it after async await gives the warning "Do not use BuildContexts across async gaps."
class GameController extends ChangeNotifier {
DrawController drawController = DrawController();
void someFunction(BuildContext context) async {
User? userData = await localStorage.getUserData();
Provider.of(context, listen: false)
.clearCanvas(); //Gives warning "Do not use BuildContexts across async gaps."
}
}
Can anyone tell me the correct way of doing it.