stateRefresh<T extends StatefulWidget> function

void stateRefresh<T extends StatefulWidget>({
  1. required State<T> state,
  2. void change()?,
})

Refreshes the TurboState remotely after checking if it is actually mounted in the widget tree. This also checks for WidgetsBinding.instance, so as to make sure that the setState method is not called while the framework is building the widget.

Implementation

void stateRefresh<T extends StatefulWidget>({
  required State<T> state,
  void Function()? change,
}) {
  try {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (state.mounted) {
        state.setState(change ?? () {});
      }
    });
  } catch (_) {
    try {
      if (state.mounted) {
        state.setState(change ?? () {});
      }
    } catch (error) {
      print(error);
    }
  }
}