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() {
  password = TextFieldState(
    onSubmitted: (s) async {
      if (s.error.value != null || s.status.value.isLoading) {
        return;
      }

      s.editable.value = false;
      s.status.value = RxStatus.loading();

      final bool hasEmail = myUser.value?.emails.confirmed.isNotEmpty == true;

      try {
        final List<Future> futures = [
          ...sessions.map((e) {
            final code = ConfirmationCode.tryParse(s.text);

            if (code == null || !hasEmail) {
              return _authService.deleteSession(
                id: e.id,
                password: s.text.isEmpty ? null : UserPassword(s.text),
              );
            }

            // Otherwise first try the parsed [ConfirmationCode].
            return Future(() async {
              try {
                await _authService.deleteSession(id: e.id, code: code);
              } on DeleteSessionException catch (ex) {
                switch (ex.code) {
                  // If wrong, then perhaps it may be a password instead?
                  case DeleteSessionErrorCode.wrongCode:
                    await _authService.deleteSession(
                      id: e.id,
                      password: s.text.isEmpty ? null : UserPassword(s.text),
                    );
                    break;

                  default:
                    rethrow;
                }
              }
            });
          }),
        ];

        await Future.wait(futures);

        stage.value = DeleteSessionStage.done;
      } on DeleteSessionException catch (e) {
        s.error.value = e.toMessage();
      } catch (e) {
        s.error.value = 'err_data_transfer'.l10n;
        rethrow;
      } finally {
        s.status.value = RxStatus.empty();
        s.editable.value = true;
      }
    },
  );

  if (myUser.value?.emails.confirmed.isNotEmpty == true) {
    sendConfirmationCode();
  }

  super.onInit();
}