当用户在Flutter中使用auth Firebase注册时如何实施用户警报

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

您好,我正在尝试找出当用户尝试登录或注册我的应用程序时如何实施警报。

下面的代码是我的“ auth-service-dart”页面,我正在尝试按照以下代码从(print(e))显示控制台中显示的消息错误,到我拥有注册按钮的注册页面,因此我想要的结果是该应用程序在用户发生错误(例如添加已添加的电子邮件或错误的电子邮件)时显示警报。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class AuthService {
  static final _auth = FirebaseAuth.instance;

  static final _firestore = Firestore.instance;

  static void signUpUser(
      BuildContext context, String name, String email, String password) async {
    try {
      AuthResult authResult = await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      FirebaseUser signedInUser = authResult.user;
      if (signedInUser != null) {
        _firestore.collection('/users').document(signedInUser.uid).setData({
          'name': name,
          'email': email,
          'profileImageUrl': '',
        });
        Navigator.pushReplacementNamed(context, signedInUser.uid);
        //Provider.of<UserData>(context).currentUserId = signedInUser.uid;
        //Navigator.pop(context);
      }
    } catch (e) {
      print(e);
    }
  }

  static void logout() {
    _auth.signOut();
    //Navigator.pushReplacementNamed(context, LoginScreen.id);
  }

  static void login(String email, String password) async {
    try {
      await _auth.signInWithEmailAndPassword(email: email, password: password);
    } catch (e) {
      print(e);
    }
  }
}
firebase flutter android-alertdialog
1个回答
0
投票

您可以使用flutter提供的showDialog;这是您的widget

中的简单代码
 showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Error happend'),
          content: Text(// your errro code here),
          actions: <Widget>[
            MaterialButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              color: Theme.of(context).primaryColor,
              child: Text(language['ok']),
            ),
          ],
        );
      },
    );
© www.soinside.com 2019 - 2024. All rights reserved.