Flutter firebase 异常没有被 try-catch 块捕获

问题描述 投票:0回答:1

我正在尝试使用flutter和firebase实现简单的登录(刚刚开始学习)。代码中的

user-not-found
块未捕获
try-catch
错误。

有问题的代码

TextButton(
  onPressed: () async {
    final email = _eMail.text;
    final password = _passWord.text;
    try {
      final userCredential =
          FirebaseAuth.instance.signInWithEmailAndPassword(
        email: email,
        password: password,
      );
      print(" UC = \n");
      print(userCredential);
    } catch (e) {
      print("Error Code: ");
      print(e.runtimeType);
      print(e);
    }
  },
  child: Text("Login"),
),

输出(在调试器中):

I/flutter (10795):  UC = 
I/FirebaseAuth(10795): Logging in as [email protected] with empty reCAPTCHA token
I/flutter (10795): Instance of 'Future<UserCredential>'
W/System  (10795): Ignoring header X-Firebase-Locale because its value was null.
E/RecaptchaCallWrapper(10795): Initial task failed for action RecaptchaAction(action=signInWithPassword)with exception - There is no user record corresponding to this identifier. The user may have been deleted.
E/flutter (10795): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_auth/user-not-found] There is no user record corresponding to this identifier. The user may have been deleted.
E/flutter (10795): #0      FirebaseAuthHostApi.signInWithEmailAndPassword (package:firebase_auth_platform_interface/src/pigeon/messages.pigeon.dart:1158:7)
E/flutter (10795): <asynchronous suspension>
E/flutter (10795): #1      MethodChannelFirebaseAuth.signInWithEmailAndPassword (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:343:22)
E/flutter (10795): <asynchronous suspension>
E/flutter (10795): #2      FirebaseAuth.signInWithEmailAndPassword (package:firebase_auth/src/firebase_auth.dart:590:9)
E/flutter (10795): <asynchronous suspension>
E/flutter (10795):

据我了解,如果错误被捕获,

print(" UC = \n");
语句不应该被执行。

请帮我解决这个问题

flutter firebase dart exception firebase-authentication
1个回答
0
投票

正如 Dar 所评论的,

signInWithEmailAndPassword
方法是异步的,因此您必须使用
await
来等待它完成:

//                      👇
final userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
  email: email,
  password: password,
);
© www.soinside.com 2019 - 2024. All rights reserved.