如何在AlertDialo中显示Firebase异常(错误)

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

我想知道如何获取Firebase异常并将其显示在Flutter中的alertDialog中。谢谢!

这是我的auth_service中的登录方法,我知道cath(e)在控制台中打印错误,但我想知道是否有任何方法可以编辑这些错误并将其显示在alertDialog中

这是我auth_servic.dart文件的类中的方法

 static void login(String email, String password) async {
    try {
      await _auth.signInWithEmailAndPassword(email: email, password: password);
    } catch (e) {
      print(e);
    }
 }

myLoginPage:

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:jobly10/screens/google_signup.dart'; import 'package:jobly10/screens/passwordRecover.dart'; import 'package:jobly10/services/auth_service.dart'; import 'package:page_transition/page_transition.dart'; import '../checkifsignedupornot.dart'; import 'google_signup.dart'; import 'package:modal_progress_hud/modal_progress_hud.dart';

bool showSpinner = false;

class Login extends StatefulWidget {   static final String id = 'login_screen';   @override   _LoginState createState() =>
_LoginState(); }

class _LoginState extends State<Login> {   bool showSpinner = false;   final _formKey = GlobalKey<FormState>();   String _email, _password;

  _submit() {
    if (_formKey.currentState.validate()) {
      _formKey.currentState.save();
      // Logging in the user w/ Firebase
      AuthService.login(_email, _password);
    }   }

  @override   Widget build(BuildContext context) {
    return Scaffold(
      body: ModalProgressHUD(
        inAsyncCall: showSpinner,
        child: Center(
          child: Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Form(
                  key: _formKey,
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Center(
                              child: Container(
                                child: Center(
                                  child: Text(
                                    'Jobly',
                                    style: TextStyle(
                                        color: Colors.redAccent,
                                        fontSize: 50,
                                        fontFamily: 'Pacifico-Regular',
                                        shadows: <Shadow>[
                                          Shadow(
                                              offset: Offset(2.0, 3.0),
                                              blurRadius: 1.0,
                                              color: Colors.black),
                                        ]),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Container(
                        height: 50,
                        width: 320,
                        child: Center(
                          child: Text(
                            'No pierdas la opertunidad. Sigue active y actualizado',
                            style: TextStyle(
                                color: Colors.black26,
                                fontFamily: 'VarelaRound-Regular',
                                fontSize: 12.0),
                          ),
                        ),
                      ),
                      Container(
                        height: 50,
                        width: 320,
                        child: TextFormField(
                          keyboardType: TextInputType.emailAddress,
                          cursorColor: Colors.black,
                          style: TextStyle(
                              fontFamily: 'Poppins-Medium',
                              fontSize: 15.0,
                              color: Colors.black),
                          decoration: InputDecoration(
                            contentPadding: const EdgeInsets.all(8.0),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(5.0),
                            ),
                            labelText: 'Correo',
                          ),
                          validator: (input) => input.trim().isEmpty
                              ? 'Ingrese un correo valido'
                              : null,
                          onSaved: (input) => _email = input,
                        ),
                      ),
                      SizedBox(height: 10.0),
                      Container(
                        height: 50,
                        width: 320,
                        child: TextFormField(
                          obscureText: true,
                          cursorColor: Colors.black,
                          style: TextStyle(
                              fontFamily: 'Poppins-Medium',
                              fontSize: 15.0,
                              color: Colors.black),
                          decoration: InputDecoration(
                            contentPadding: const EdgeInsets.all(8.0),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(5.0),
                            ),
                            labelText: 'Contraseña',
                          ),
                          validator: (input) => input.trim().isEmpty
                              ? 'Ingrese una contraseña valida'
                              : null,
                          onSaved: (input) => _password = input,
                        ),
                      ),
                      SizedBox(height: 10.0),
                      Container(
                        height: 50,
                        width: 320,
                        decoration: BoxDecoration(
                          gradient: LinearGradient(colors: [
                            Colors.black87,
                            Colors.black87,
                          ]),
                          borderRadius: BorderRadius.circular(6.0),
                        ),
                        margin: EdgeInsets.symmetric(
                            vertical: 0.0, horizontal: 0.0),
                        child: Material(
                          color: Colors.transparent,
                          child: InkWell(
                            highlightColor: Colors.deepOrange,
                            splashColor: Colors.black87,
                            onTap: () {
                              setState(() {
                                showSpinner = true;
                              });
                              _submit();
                            },
                            child: Center(
                              child: Text(
                                "Ingresar",
                                style: TextStyle(
                                    color: Colors.white,
                                    backgroundColor: null,
                                    fontFamily: "CM Sans Serif",
                                    fontSize: 12,
                                    letterSpacing: 1.0),
                              ),
                            ),
                          ),
                        ),
                      ),
                      SizedBox(height: 20.0),
                      Container(
                        height: 15,
                        width: 320,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Expanded(
                              child: Container(
                                height: 1,
                                color: Colors.black26,
                              ),
                            ),
                            SizedBox(
                              width: 10.0,
                            ),
                            Center(
                              child: Text(
                                'or',
                                style: TextStyle(
                                    color: Colors.black26, fontSize: 13.0),
                              ),
                            ),
                            SizedBox(
                              width: 10.0,
                            ),
                            Expanded(
                              child: Container(
                                height: 1,
                                color: Colors.black26,
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(height: 15.0),
                      InkWell(
                        child: Text(
                          "Registrase.",
                          style: TextStyle(
                              color: Colors.black,
                              fontFamily: "CM Sans Serif",
                              fontSize: 13,
                              letterSpacing: 1.0),
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            PageTransition(
                              type: PageTransitionType.upToDown,
                              child:
                                  CheckingSignup(), //will pass the route to the CheckIfSignedupOrNot Class
                              duration: Duration(milliseconds: 200),
                            ),
                          );
                        },
                      ),
                      InkWell(
                        child: Text(
                          "Perdi mi contraseña.",
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) {
                                return PasswordRecover();
                              },
                            ),
                          );
                        },
                      ),
                      InkWell(
                        child: Text(
                          "Google Sign In",
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) {
                                return GoogleSigninSignUp();
                              },
                            ),
                          );
                        },
                      ),
                      InkWell(
                        child: Text(
                          "Ingresar con numero de telefono",
                        ),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) {
                                return CheckingPhoneLogin();
                              },
                            ),
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
   } }
firebase flutter android-alertdialog
1个回答
0
投票

欢迎使用stackoverflow:)

这是我用来处理所有错误的AlertDialog:

void error(String error, BuildContext context,
    {Color backgroundColor = Colors.white,
    TextAlign textAlign = TextAlign.start,
    double fontSize = 20,
    FontWeight fontWeight = FontWeight.bold,
    Color textColor = Colors.red}) {
  AlertDialog errorAlertDialog = new AlertDialog(
      elevation: 0,
      backgroundColor: backgroundColor,
      content: SelectableText(
        error,
        textAlign: textAlign,
        style: TextStyle(
            color: textColor, fontSize: fontSize, fontWeight: fontWeight),
      ));

  showDialog(context: context, child: errorAlertDialog);
}

[您需要给他您的error.toString()(在您的情况下为e.toString())和实际上下文。

© www.soinside.com 2019 - 2024. All rights reserved.