超时函数仍然执行操作

Timeout function still executes the operation

我正在尝试创建一种方法,用户可以在其中更改它的 firebase_authentication display name 并附带一个 timeout 函数:

Future<void> changeDisplayName({String name}) async {
    try {
      await _auth.currentUser
          .updateProfile(displayName: name)
          .timeout(Duration(seconds: 10))
          .then((value) => _createSnackbar(
              'Update Successfull', 'Your new display name is $name'));
    } on TimeoutException {
      Navigator.pop();
      _createSnackbar(
          'Timeout Exception', 'Operation stopped.');
    } catch (e) {
      _createSnackbar('Error Occurred', '${e.toString()}');
    }
  }

我的意图是当抛出超时异常时,完全停止操作。但是即使抛出超时异常, profile update 仍然在超时后完成。调用timeout时如何避免操作?

无法取消 updateProfile 操作。进行方法调用后,您必须等待它完成并调用 thencatch 来确定其结果。

如果您尝试处理用户不在网络上的情况,您可能希望在调用 API 之前检测该情况。但就我个人而言,我会考虑只向用户发出实际情况的信号:“这花费的时间比预期的要长,您可能想稍后再试。”