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

Flutter FirebaseAuthException : Display custom error messages

我很难在 login/sign 期间显示我的自定义错误消息。显示的错误消息是来自 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();
              },
            ),
          ],
        ),
      );

将其替换为 message,它应该可以工作。