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

时间:2020-02-19 20:27:46

标签: firebase flutter android-alertdialog

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

下面的代码是我的“ 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);
    }
  }
}

1 个答案:

答案 0 :(得分: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']),
            ),
          ],
        );
      },
    );