android try/catch阻塞在dart/flutter中不起作用

xe55xuns  于 2022-12-09  发布在  Android
关注(0)|答案(2)|浏览(140)

我有一个例外:

class WrongHourException implements Exception {}

我有一个函数可以检查很多事情,比如检查小时:

void confrimSchedule({
  required String startHour,
  required String startMinute,
  required String endHour,
  required String endMinute,
  required String periodMinute,
  required String day,
}) async {
  late double start;
  late double end;
  late double period;

  //check the validation of hours
  if (int.parse(startHour) > 24 || int.parse(startHour) < 0) {
    throw WrongHourException();
  } else if (int.parse(endHour) > 24 || int.parse(endHour) < 0) {
    throw WrongHourException();
  } else if (int.parse(endHour) < int.parse(startHour)) {
    throw WrongHourException();
  }
  //check the validation of start minute
  if (startMinute == "" || startMinute == "00" || startMinute == "0") {
    start = double.parse(startHour);
  } else if (startMinute == "15") {
    start = double.parse("$startHour.25");
  } else if (startMinute == "30") {
    start = double.parse("$startHour.5");
  } else if (startMinute == "45") {
    start = double.parse("$startHour.75");
  } else {
    throw WrongeMinuteException();
  }
  //check the validation of end minute
  if (endMinute == "" || endMinute == "00" || endMinute == "0") {
    end = double.parse(endHour);
  } else if (endMinute == "15") {
    end = double.parse("$endHour.25");
  } else if (endMinute == "30") {
    end = double.parse("$endHour.5");
  } else if (endMinute == "45") {
    end = double.parse("$endHour.75");
  } else {
    throw WrongeMinuteException();
  }
  //check the validation of preoid of each turn
  if (periodMinute == "30") {
    period = 0.5;
  } else if (periodMinute == "45") {
    period = 0.75;
  } else if (periodMinute == "60") {
    period = 1;
  } else if (periodMinute == "90") {
    period = 1.5;
  } else {
    throw WrongePeriodException();
  }

  //generate new schedule
  List newTurns = generateTurns(start: start, end: end, period: period);
  //update new schedule in cache
  TurnData().turns[day]!.clear();
  for (String time in newTurns) {
    TurnData().turns[day]!.add(
          Turn(
            time: time,
            isAvailable: false,
            name: "",
            number: "0",
          ),
        );
  }
  //update database
  await ServerService.updateDatabase(attr: day, value: [
    DateData().idate[day]!.showDate(),
    TurnData().dataBaseStructure(day),
  ]);
}

这就是我想使用try/catch地方:

// update scheduel
on<ScheduelEventUpdateScheduel>((event, emit) async {
  try {
    if (haveNumber(event.day)) {
      bool isSure = await showAlertDialog(
        context: event.context,
        content: "با تغییر ساعت کاری نوبت ها لغو می شوند. آیا مطمئن هستید؟",
      );
      if (isSure) {
        emit(ScheduelStateDayView(
          day: event.day,
          isLoading: false,
          isScheduelLoading: true,
        ));
        sendCancleNotifications(
          day: event.day,
          context: event.context,
          dayP: event.dayP,
        );
         confrimSchedule(
          startHour: event.startHour,
          startMinute: event.startMinute,
          endHour: event.endHour,
          endMinute: event.endMinute,
          periodMinute: event.period,
          day: event.day,
        );
        emit(ScheduelStateDayView(
          day: event.day,
          isLoading: false,
          isScheduelLoading: false,
        ));
      }
    } else {
      bool isSure = await showAlertDialog(
        context: event.context,
        content: "آیا می خواهید ساعت کاری خود را تغییر دهید؟",
      );
      if (isSure) {
        emit(ScheduelStateDayView(
          day: event.day,
          isLoading: false,
          isScheduelLoading: true,
        ));

        confrimSchedule(
          startHour: event.startHour,
          startMinute: event.startMinute,
          endHour: event.endHour,
          endMinute: event.endMinute,
          periodMinute: event.period,
          day: event.day,
        );
        emit(ScheduelStateDayView(
          day: event.day,
          isLoading: false,
          isScheduelLoading: false,
        ));
      }
    }
  }  on Exception catch (e) {
    emit(ScheduelStateDayView(
      day: event.day,
      isLoading: false,
      isScheduelLoading: false,
      exception: e,
    ));
  }
});

我正在对其他一些函数使用async/await,而不是confrimScheduel(在scheduel_bloc中)或检查小时的验证(在scheduel_functions中)。
但我得到一个错误消息,告诉我Unhandled Exception : Instance of 'WrongHourException'为什么try/catch不起作用?

wooyq4lh

wooyq4lh1#

当你打电话

confrimSchedule( // line 161 scheduel_bloc.dart
          startHour: event.startHour,
          startMinute: event.startMinute,
          endHour: event.endHour,
          endMinute: event.endMinute,
          periodMinute: event.period,
          day: event.day,
        );

等待结果。将该行更改为

await confrimSchedule( // line 161 scheduel_bloc.dart
          startHour: event.startHour,
          startMinute: event.startMinute,
          endHour: event.endHour,
          endMinute: event.endMinute,
          periodMinute: event.period,
          day: event.day,
        );

confrimSchedule( // line 161 scheduel_bloc.dart
          startHour: event.startHour,
          startMinute: event.startMinute,
          endHour: event.endHour,
          endMinute: event.endMinute,
          periodMinute: event.period,
          day: event.day,
        ).then((_){
//emit here
});
pxiryf3j

pxiryf3j2#

代替

on Exception catch (e) {
    emit(ScheduelStateDayView(
      day: event.day,
      isLoading: false,
      isScheduelLoading: false,
      exception: e,
    ));   }

试试看:

on WrongHourException catch (e) {
    emit(ScheduelStateDayView(
      day: event.day,
      isLoading: false,
      isScheduelLoading: false,
      exception: e,
    ));   }

相关问题