【问题标题】:Show Exception Errors in UI Firebase Auth Flutter在 UI Firebase Auth Flutter 中显示异常错误
【发布时间】:2020-07-17 19:58:45
【问题描述】:

首先我是 Flutter 的新手,我阅读了很多线程但没有得到任何东西。

我的问题是什么?

我有两个页面 auth.dartlogin.dart。在 auth.dart 页面中我有一个名为 AuthService 的类 由以下代码组成(直截了当):

class AuthService {

  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future<bool> signInWithPhone(String phone, BuildContext context) async {
  ....
      verificationFailed: (AuthException exception) {
        // i can see the exception in debug console using print
        return exception;
      },
  ....
  }

}

login.dart 中,我有一个有状态的小部件,它看起来像这样,在那里我引用了上面的类,我正在调用 signInWithPhone 方法: p>

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {

  final AuthService _auth = AuthService();
  ....
  ....
  Container(
    width: double.infinity,
    child: FlatButton(
        child: Text("LOGIN"),
        textColor: Colors.white,
        padding: EdgeInsets.all(16),
        onPressed: () {
            // here i am calling the method signInWithPhone of above class

            final phone = _phoneController.text.trim();
            _auth.signInWithPhone(phone, context);

        }
    )
  )
 ....
 ....
}

我想要什么?

我想显示在我的登录按钮的 onPressed 事件中收到的异常错误消息,以便我可以使用错误消息显示警报对话框。

我尝试了什么?

1) print(_auth.signInWithPhone(phone, context)); 但它返回null;

2)

_auth.signInWithPhone(phone, context).then((value){
   // alert/text/print
   print(value);
   }

这也返回 null

任何人都可以帮我解决它,因为如果手机号码不正确或 OTP 不正确,我需要向用户显示错误消息。

【问题讨论】:

    标签: firebase flutter dart firebase-authentication


    【解决方案1】:

    嗯,首先你需要监听异常并等待它,你可以在期望的情况下返回 false 并根据这种情况做一些事情,就像这样。

    你可以返回一个包含结果的地图和这样的消息

    {
      'message': message,
      'success' : true or false depending on the state of the request
    }
    
    

    如此

    AuthService.dart

     verificationFailed: (AuthException exception) {
            // i can see the exception in debug console using print
            //return exception;
            return {
                'message': exception,
               'success': false
             }; // means verification failed;
     },
    
    
    

    LoginScreen.dart

    
    void showAlert(message) {
        showDialog(context: context, builder: (context) {
           return AlertDialog(
             title: message,
           );
        });
    }
    
     onPressed: async () {
                // here i am calling the method signInWithPhone of above class
    
                final phone = _phoneController.text.trim();
                final result = await _auth.signInWithPhone(phone, context);
    
    
                if(!result['suceess']) {
                     this.showAlert(result['message']);
                 }
            }
    
    

    【讨论】:

    • 感谢您的回答,它有效但是如果我想显示验证失败方法的错误消息而不是在 LoginScreen 中显示我的硬编码错误,还有另一件事?有没有可能这样做?
    • @MR_AMDEV 我更新了我的答案以反映您想要的更改
    • 它给了我以下错误The operator '[]' isn't defined for the class 'bool'. Try defining the operator '[]'.dart(undefined_operator)
    • 哦,你需要将你的方法签名从Future&lt;bool&gt; signInWithPhone更新为Future&lt;Map&gt; signInWithPhone
    • 我已经更改了它,但现在在运行时它给了我以下错误:Exception has occurred. NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("success"))
    猜你喜欢
    • 2021-01-12
    • 2021-09-26
    • 2019-09-10
    • 2021-04-22
    • 2022-12-31
    • 2023-01-15
    • 1970-01-01
    • 2021-03-24
    • 2022-09-25
    相关资源
    最近更新 更多