toggle method

Future<void> toggle (
  1. {@required bool enable,
  2. @Deprecated('Use the `enable` parameter instead.') bool on}
)

Toggles the wakelock on or off.

You can simply use this function to toggle the wakelock using a bool value (for the enable parameter).

// This line keeps the screen on.
Wakelock.toggle(enable: true);

bool enableWakelock = false;
// The following line disables the wakelock.
Wakelock.toggle(enable: enableWakelock);

You can await the Future to wait for the operation to complete.

Implementation

static Future<void> toggle({
  @required bool enable,
  @Deprecated('Use the `enable` parameter instead.') bool on,
}) {
  // The checks allow only `on` to be used in the case of old code and
  // they encourage to use only `enable` instead (combined with the
  // deprecation warning).
  assert(enable != null || on != null,
      'The `enable` parameter must not be null when toggling the wakelock.');
  assert(
      on == null || enable == null,
      'The `on` parameter has been deprecated; '
      'specify only the `enable` parameter instead.');

  return _wakelockPlatformInstance.toggle(enable: enable ?? on);
}