onInit method

  1. @override
void onInit()
override

Called immediately after the widget is allocated in memory. You might use this to initialize something for the controller.

Implementation

@override
void onInit() {
  code = TextFieldState(
    onFocus: (s) {
      if (s.text.isNotEmpty) {
        try {
          ConfirmationCode(s.text);
        } on FormatException {
          s.error.value = 'err_wrong_recovery_code'.l10n;
        }
      }
    },
    onSubmitted: (s) async {
      final code = ConfirmationCode.tryParse(s.text);

      if (code == null) {
        s.error.value = 'err_wrong_recovery_code'.l10n;
      } else {
        s.editable.value = false;
        s.status.value = RxStatus.loading();
        try {
          await _myUserService.addUserEmail(
            email!,
            confirmation: code,
            locale: L10n.chosen.value?.toString(),
          );
          pop?.call();
          s.clear();
        } on AddUserEmailException catch (e) {
          if (e.code == AddUserEmailErrorCode.occupied) {
            s.resubmitOnError.value = true;
          }

          s.error.value = e.toMessage();
        } catch (e) {
          s.resubmitOnError.value = true;
          s.error.value = 'err_data_transfer'.l10n;
          s.unsubmit();
          rethrow;
        } finally {
          s.editable.value = true;
          s.status.value = RxStatus.empty();
        }
      }
    },
  );

  super.onInit();
}