【问题标题】:Trouble with Dart Futures in Flutter : Failed assertion: line 146: '<optimized out>': is not trueFlutter 中的 Dart Futures 问题:断言失败:第 146 行:'<optimized out>': is not true
【发布时间】:2019-04-11 14:17:37
【问题描述】:

我正在为我的应用程序构建一个用户身份验证模块,但我遇到了一些异步代码的问题。

首先,这是抛出的错误:

E/flutter (17162): [ERROR:flutter/shell/common/shell.cc(188)] Dart 错误:未处理的异常: E/flutter(17162):“dart:async/future_impl.dart”:断言失败:第 146 行:“优化”:不正确。 E/flutter (17162): #0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:40:39) E/flutter (17162): #1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:36:5) E/flutter (17162): #2 _FutureListener.handleError (dart:async/future_impl.dart:146:14) E/flutter (17162): #3 Future._propagateToListeners.handleError (dart:async/future_impl.dart:654:47) E/flutter (17162): #4 Future._propagateToListeners (dart:async/future_impl.dart:675:24) E/flutter (17162): #5 Future._completeError (dart:async/future_impl.dart:494:5) E/flutter (17162): #6 _SyncCompleter._completeError (dart:async/future_impl.dart:55:12) E/flutter (17162): #7 _Completer.completeError (dart:async/future_impl.dart:27:5) E/flutter (17162): #8 _AsyncAwaitCompleter.completeError (dart:async/runtime/libasync_patch.dart:40:18) E/flutter (17162):#9 FirebaseAuth.signInWithEmailAndPassword(包:firebase_auth/firebase_auth.dart) E/颤振(17162): E/flutter (17162):#10 Session.login。 (包:mood_map/utilities/session.dart:31:24) E/flutter (17162): #11 _RootZone.runUnary (dart:async/zone.dart:1379:54) E/flutter (17162): #12 _FutureListener.handleValue (dart:async/future_impl.dart:129:18) E/flutter (17162): #13 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45) E/flutter (17162): #14 Future._propagateToListeners (dart:async/future_impl.dart:671:32) E/flutter (17162): #15 Future._complete (dart:async/future_impl.dart:476:7) E/flutter (17162): #16 _SyncCompleter.complete (dart:async/future_impl.dart:51:12) E/flutter (17162): #17 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18) E/flutter (17162): #18 _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:295:13) E/flutter (17162): #19 Session._checkUserAlreadyExists (package:mood_map/utilities/session.dart) E/颤振(17162): E/flutter (17162): #20 Session.login (package:mood_map/utilities/session.dart:27:11)

这里是涉及的功能:

static final FirebaseAuth _authenticator = FirebaseAuth.instance;

static void login(BuildContext context, String email, String password) async {

email = email.trim();
password = password.trim();

//Check if the user already exists
await _checkUserAlreadyExists(email).then((exists) {

  if(exists) {

    _authenticator.signInWithEmailAndPassword(email: email, password: password)
        .then((FirebaseUser user) { _loginSuccess(); })
        .catchError((Error e) { _loginFailure(context); });

  } else {

    Utilities.showMessageDialog(context, "That user doesn't exist. Please create an account below.");

  }

});

} 

----------------------------------------------------------------------

static Future createUserAccount(BuildContext context, email, String password) async {

//Check if the user already exists
await _checkUserAlreadyExists(email).then((exists) {

  if(exists) {

    Utilities.showMessageDialog(context, "That user already exists. Please login or select another account.");
    AppNavigator.navigateToLoginScreen();

  } else {

    _authenticator.createUserWithEmailAndPassword(email: email, password: password)
        .then((FirebaseUser user) { _createUserSuccess(); })
        .catchError((Error e) { _createUserFailure(context); });

  }

});

}

简而言之,对 _authenticator.signonWithEmailAndPassword() 的调用失败。我知道 _authenticator 实例正在使用其他函数,所以我知道这不是 Firebase 本身的问题。

我担心我从另一个异步函数 _checkIfUserAlreadyExists() 中调用另一个异步函数 _authenticator.signonWithEmailAndPassword() 做错了事。从我读过的内容来看,这似乎可以在 .then() 块中执行,但错误消息似乎非常坚持认为这与函数调用的异步性质的设置有关。

想法?

【问题讨论】:

  • 上述问题你得到解决方案了吗?
  • 没有。我做了更多的研究,这似乎是当前版本的 Flutter 的系统性问题。我发现了一个报告它的 github 问题,以便开发人员知道。我也发布了我的发现。见:github.com/flutter/flutter/issues/23512

标签: asynchronous dart async-await flutter


【解决方案1】:

如果您使用.then() 子句,请不要使用await

.then()await 是处理Future 的两种不同方式,但不应用于相同的Future 实例。

【讨论】:

    【解决方案2】:

    考虑使用 async - await 来捕获“最后一步”中的错误。这个回答https://github.com/flutter/flutter/issues/22734对我帮助很大。

    以下是我从一个我不记得的来源获得的代码 sn-p,但它帮助我了解了如何正确使用 Futures。我对其进行了一些修改以测试我的确切情况(添加了 ma​​in4()divideFullAsyncNested() 函数)。希望对你有帮助。

    // SO 29378453
    import 'dart:async';
    
    import 'package:login_app/constants.dart';
    
    main() {
      // fails
      main1();
      main2();
      main3();
      main4();
    }
    
    Future<double> divide(int a, b) {
      // this part is still sync
      if (b == 0) {
        throw new Exception('Division by zero divide non-async');
      }
      // here starts the async part
      return new Future.value(a / b);
    }
    
    Future<double> divideFullAsync(int a, b) {
      return new Future(() {
        if (b == 0) {
          throw new Exception('Division by zero full async');
        }
        return new Future.value(a / b);
        // or just
        // return a / b;
      });
    }
    
    Future<double> divideFullAsyncNested() {
      return divideFullAsync(7, 8).then(
        (val) {
          return divideFullAsync(5, 0).then(
            (val2) {
              return Future(() {
                if (val2 == 1) {
                  throw Exception('Innermost: Result not accepted exception.');
                }
                return val2;
              });
            },
          ).catchError((err) => throw Exception('Inner:    $err'));
        },
      ).catchError((err) => throw Exception('Outter: $err'));
    }
    
    //Future<double> divideFullAsyncNested() {
    //  return divideFullAsync(9, 9).then(
    //    (val) {
    //      return Future(
    //        () {
    //          if (val == 1) {
    //            throw Exception('Result not accepted exception.');
    //          }
    //          return val;
    //        },
    //      );
    //    },
    //  ).catchError((err) => throw Exception(err.toString()));
    //}
    
    // async error handling doesn't catch sync exceptions
    
    void main1() async {
      try {
    //    divide(1, 0).then((result) => print('(1) 1 / 0 = $result')).catchError(
    //        (error) => print('(1)Error occured during division: $error'));
        var result = await divide(1, 0);
        print('(1) 1 / 0 = $result');
      } catch (ex) {
        print('(1.1)Error occured during division: ${ex.toString()}');
      }
    }
    
    // async error handling catches async exceptions
    void main2() {
      divideFullAsync(1, 0)
          .then((result) => print('(2) 1 / 0 = $result'))
          .catchError(
              (error) => print('(2) Error occured during division: $error'));
    }
    
    // async/await allows to use try/catch for async exceptions
    main3() async {
      try {
        await divideFullAsync(1, 0);
        print('3');
      } catch (error) {
        print('(3) Error occured during division: $error');
      }
    }
    
    main4() async {
    //  try {
    //    await divideFullAsyncNested();
    //  } on Exception catch (e) {
    //    print("(4) ${e.toString().replaceAll('Exception:', '').trimLeft().trimRight()}");
    //  }
      try {
        divideFullAsyncNested()
            .then((v) => print(v))
            .catchError((err) => print(Constants.refinedExceptionMessage(err)));
      } on Exception catch (e) {
        print("(4) ${Constants.refinedExceptionMessage(e)}");
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2020-06-05
      • 2020-11-30
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多