gerar method

Future<void> gerar()

Gera um código. Com uma geração em curso, ou no limite de alunos, não faz nada (no limite, avisa).

Implementation

Future<void> gerar() async {
  if (isClosed || state.gerando) return;
  if (limiteDeAlunosAtingido(state.ocupados)) {
    _avisar(textoDoLimiteDeAlunos);
    return;
  }
  emit(state.copyWith(gerando: true));
  try {
    final convite = await remote
        .convidar(profUid: profUid, nome: nome)
        .timeout(esperaMaxima);
    if (!isClosed) {
      emit(state.copyWith(convite: convite, ocupados: state.ocupados + 1));
    }
  } on TimeoutException {
    _avisar(
      'Sem conexão: não deu para gerar o código. Tente quando tiver internet.',
    );
  } on TurmaException catch (e) {
    _avisar(e.mensagem);
  } on Object catch (e) {
    _avisar('Não consegui gerar o código: $e');
  } finally {
    if (!isClosed) emit(state.copyWith(gerando: false));
  }
}