fixPowerSettings method Null safety

dynamic fixPowerSettings(
  1. {required String title,
  2. required String powerSavingError,
  3. required String batteryOptimizationError,
  4. dynamic errorHandler(
    1. String,
    2. String
    )?}
)

Optimize device power settings by calling showPowerSaving and showBatteryOptimizations sequentially.

If a step fails the errorHandler will be called with the provided title and either powerSavingError or batteryOptimizationError depending on the step that failed.

If no errorHandler is provided a confirmation dialog is shown with the title and appropriate message for the step failed.

Implementation

fixPowerSettings({
  required String title,
  required String powerSavingError,
  required String batteryOptimizationError,
  Function(String, String)? errorHandler,
}) async {
  /// Error message to be shown if step fails
  late String message;
  try {
    message = powerSavingError;
    await showPowerSaving();

    message = batteryOptimizationError;
    await showBatteryOptimizations();

    // coverage:ignore-start
  } on Exception catch (error) {
    // Depending on Manufacturer/Model/OS Version, a Device may not implement
    // a particular Settings screen.
    print(error);
    errorHandler ??= _onFixPowerSettingsFailed;
    errorHandler(title, message);
  }
}