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



你好,我正在尝试了解如何在用户尝试登录或注册我的应用程序时实现警报。

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

您可以使用flutter提供的showDialog;这是小部件中的一个简单代码

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']),
),
],
);
},
);

最新更新