buttonDown method Null safety

  1. @override
void buttonDown(
  1. Operation key
)
override

Implementation

@override
void buttonDown(Operation key) {
  model.onIsPressed.value = false;
  if (key == Operations.onOff) {
    changeState(DoNothing(controller));
    Future<void> res = () async {
      try {
        await model.writeToPersistentStorage();
      } finally {
        if (!kIsWeb && Platform.isLinux) {
          // Current version on Linux dumps core on pop.
          // Current version on Windows keeps window there.
          exit(0);
        } else {
          await SystemNavigator.pop();
          // That kills us on some platforms, but it doesn't make sense on
          // web.  It's a NOP on iOS, because apps terminating themselves
          // is against the Apple human interface guidelines (which don't
          // make a bit of sense if you're turning a calculator off, but
          // whatever.)  So, on platforms were we can't go away, we blank
          // the LCD display and wait for the ON button to be pressed.
          model.display.show(LcdContents.blank());
          changeState(CalculatorOff(controller));
        }
      }
    }();
    unawaited(res);
  } else if (key == Operations.dot) {
    model.settings.euroComma = !model.settings.euroComma;
    model.display.displayX();
    changeState(Resting(controller));
  } else if (key == controller.minusOp) {
    controller.reset();
    model.reset();
    final r = changeState(Resting(controller));
    model.display.current = 'pr error ';
    model.display.update();
    changeState(MessageShowing(r));
  } else if (key == controller.multOp) {
    changeState(DoNothing(controller));
    Future<void> runTests() async {
      try {
        DateTime start = DateTime.now();
        model.display.current = '  RuNNING';
        model.display.update(blink: BlinkMode.justDigits);
        await controller.newSelfTests(inCalculator: true).runAll();
        DateTime now = DateTime.now();
        Duration sleep =
            (const Duration(milliseconds: 2500)) - now.difference(start);
        if (!sleep.isNegative) {
          await Future<void>.delayed(sleep);
        }
        changeState(Resting(controller));
        model.display.show(model.selfTestContents());
        changeState(MessageShowing(Resting(controller)));
      } on CalculatorError catch (e, s) {
        changeState(Resting(controller));
        controller.showCalculatorError(e, s);
        // ignore: avoid_catches_without_on_clauses
      } catch (e, s) {
        debugPrint('Unexpected exception $e\n\n$s');
        changeState(Resting(controller));
        controller.showCalculatorError(CalculatorError(9), s);
      }
    }

    unawaited(runTests());
  } else {
    model.display.displayX();
    changeState(Resting(controller));
  }
}