我正在尝试获取使用电子邮件和密码登录的firebase auth异常。在我的应用程序中,所有的身份验证服务都在另一个文件AuthService.dart中的AuthService类中,我想将异常从那里转移到另一个文件中的logingreen类
这是我的authservice.dart文件中的类
异常在这里起作用,我可以将其打印到控制台。
class AuthService {
// final FirebaseAuth _auth = FirebaseAuth.instance;
// UserModel _userFromFirebaseUser(User user) {
// return user != null ? UserModel(uid: user.uid) : null;
}
// Stream<UserModel> get user {
// return _auth.authStateChanges().map(_userFromFirebaseUser);
// }
Future loginWithEmailpasswd(String email, String password) async {
try {
return await _auth.signInWithEmailAndPassword(
email: email, password: password);
} on FirebaseAuthException catch (e) {
print(e.toString());
StudentLoginScreen(
message: e.toString(), // passing the exception
);
}
}
这是我的登录绿色
class StudentLoginScreen extends StatefulWidget {
final Function toggleView;
final String message;
StudentLoginScreen({this.toggleView, this.message});
@override
_StudentLoginScreenState createState() => _StudentLoginScreenState();
}
final _formkey = GlobalKey<FormState>();
class _StudentLoginScreenState extends State<StudentLoginScreen> {
String email = '';
String password = '';
String _message;
final AuthService _authService = AuthService();
//SharedPreferences usertype;
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: HexColor(studentPrimaryColour),
body: SafeArea(
child: SingleChildScrollView(
child: Form(
key: _formkey,
child: Column(
children: <Widget>[
showAlert(), // i want to show the exception message in this widget
SizedBox(
height: 25.0,
),
// HeadingText(
// text: 'Login',
// size: 60.0,
// color: Colors.white,
// ),
// RoundedInputField(
// hintText: "Email",
// validator: (val) =>
// val.isEmpty ? 'Oops! you left this field empty' : null,
// onChanged: (val) {
// email = val;
// },
// ),
// RoundedInputField(
// hintText: "Password",
// validator: (val) =>
// val.isEmpty ? 'Oops! you left this field empty' : null,
// boolean: true,
// onChanged: (val) {
// password = val;
// },
// ),
Container(
child: FlatButton(
color: Colors.white,
onPressed: () async {
if (_formkey.currentState.validate()) {
dynamic result = await _authService
.loginWithEmailpasswd(email, password);
if (result != null) {
print('logged in');
} else {
print('error logging in');
setState(() {
_message = widget.message;
});
print(_message); // its returning a null value
}
}
},
child: Text(
'login',
style: GoogleFonts.montserrat(
color: HexColor(studentPrimaryColour),
fontSize: 20),
),
),
),
),
),
],
),
),
),
),
);
}
Widget showAlert() {
if (_message != null) { //nothing happens here since _message is always null even if an exception
return Container( // is thrown in authservice
color: Colors.amber,
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Icon(Icons.error_outline_rounded),
Expanded(
child: AutoSizeText(
_message,
maxLines: 3,
))
],
),
);
}
return SizedBox(
height: 0,
);
}
}
我错过了什么重要的东西吗?我相对来说是个新手,我发现很难将值从一个类传递到另一个类。
尝试返回异常,而不是在屏幕中传递它
Future loginWithEmailpasswd(String email, String password) async {
try {
return await _auth.signInWithEmailAndPassword(
email: email, password: password);
} on FirebaseAuthException catch (e) {
print(e.toString());
return e;
}
//屏幕登录
onPressed: () async {
if (_formkey.currentState.validate()) {
dynamic result = await _authService
.loginWithEmailpasswd(email, password);
if (result is bool) {
if (result) {
print('logged in');
}}
else {
print('error logging in');
setState(() {
_message =result;
});
print(result);
}
}
},