updateState method
Calls setState safely only if the state is mounted.
Optionally, you can provide a callback to update local variables.
Example:
class _MyWidgetState extends State<MyWidget> {
int counter = 0;
void increment() {
updateState(() {
counter++;
});
}
}
Implementation
void updateState([VoidCallback? callback]) {
if (mounted) setState(callback ?? () {});
}