簡體   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