繁体   English   中英

Flutter FirebaseAuthException:显示自定义错误消息

[英]Flutter FirebaseAuthException : Display custom error messages

我正在努力在登录/注册期间显示我的自定义错误消息。 显示的错误消息是来自 Firebase 的默认消息。 我需要根据错误代码在下面的代码中显示自定义错误消息。 请协助...

我不确定还缺少什么。 我已经尝试了一切,但仍然没有获胜

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import '../widgets/auth/auth_form.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class AuthScreen extends StatefulWidget {
  const AuthScreen({Key? key}) : super(key: key);
  static const routeName = '/auth_screen';
  @override
  _AuthScreenState createState() => _AuthScreenState();
}

class _AuthScreenState extends State<AuthScreen> {
  final _auth = FirebaseAuth.instance;
  var _isLoading = false;

  void _submitAuthForm(
    String email,
    String password,
    String displayName,
    String phoneNumber,
    bool isLogin,
    BuildContext ctx,
  ) async {
    UserCredential authResult;

    try {
      setState(() {
        _isLoading = true;
      });
      if (isLogin) {
        authResult = await _auth.signInWithEmailAndPassword(
          email: email,
          password: password,
        );
      } else {
        authResult = await _auth.createUserWithEmailAndPassword(
            email: email, password: password);

        await FirebaseFirestore.instance
            .collection('userProfile')
            .doc(authResult.user!.uid)
            .set(
          {
            'displayName': displayName,
            'email': email,
            'phoneNumber': phoneNumber,
            'uid': authResult.user!.uid,
          },
        );
      }
    } on FirebaseAuthException catch (error) {
      var message = 'An error has occured.'; // default message
      switch (error.code) {
        case 'EMAIL_NOT_FOUND':
          message = 'There is no user account with the email address provided.';
          break;
        case 'EMAIL_EXISTS':
          message = 'The email address is already in use by another account.';
          break;
        case 'INVALID_PASSWORD':
          message = 'Invalid password. Please enter correct password.';
          break;
      }
      setState(() {
        _isLoading = false;
      });
      showDialog(
        context: context,
        builder: (context) => CupertinoAlertDialog(
          title: const Text('An error occured'),
          content: Text(
            error.message.toString(),
          ),
          actions: [
            TextButton(
              child: const Text('Close'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      );
    }
  }

我认为您的问题在于,当您定义了一个消息变量时,您并没有在对话框中使用它。 您正在使用error.message.toString()

 showDialog(
        context: context,
        builder: (context) => CupertinoAlertDialog(
          title: const Text('An error occured'),
          content: Text(
            error.message.toString(),  /// <-------------- HERE
          ),
          actions: [
            TextButton(
              child: const Text('Close'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      );

消息替换它,它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM