如何解决当 FirebaseAuth 实例启动时,它会在 Flutter 表单上出错
这是我的第一个 Flutter 项目,我对某些事情感到困惑。我创建了这个表单,每当我启动 Firebase 实例时,表单就会消失。
如果我删除此代码 final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
它工作正常,表单将显示但如果取消注释表单消失并返回此错误:
用于空值的空检查运算符
代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter/cupertino.dart';
import 'dart:core';
import 'package:email_validator/email_validator.dart';
class CreateLogin extends StatefulWidget {
final Function goToHomePage;
CreateLogin({this.goToHomePage});
@override
_CreateLoginState createState() => _CreateLoginState();
}
class _CreateLoginState extends State<CreateLogin> {
bool _termsAgreed = false;
String email,password,confirmPassword;
bool saveAttempted = false;
final formKey = GlobalKey<FormState>();
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
void _createuser({String email,String password}) {
_firebaseAuth.createuserWithEmailAndPassword(email: email,password: password).then((authResult) {
print(authResult.user);
}).catchError((error){
print(error);
});
}
@override
Widget build(BuildContext context) {
return Form(
key: formKey,child: Container(
child: Column(
children: <Widget>[
Text('SIGN UP',style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 26.0),),SizedBox(height: 5,TextFormField( autovalidate: saveAttempted,autovalidateMode: AutovalidateMode.always,validator: (emailValue) {
if(emailValue.isEmpty){
return "This email field cannot be empty";
}return null;
},onChanged: (textVlaue) {
setState(() {
email = textVlaue;
});
},decoration: Inputdecoration(errorStyle: TextStyle(color: Colors.white),hintText: 'Enter Email',hintStyle: TextStyle(color: Colors.white.withOpacity(0.6)),border: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),)),style: TextStyle(fontSize: 22,color: Colors.white),validator: (password) {
if(password.isEmpty){
return "This email field cannot be empty";
} if(password.length < 8){
return "Password must be greater han 8 character";
} if(EmailValidator.validate(email) != true){
return "Invalid email";
}return null;
},onChanged: (textVlaue) {
setState(() {
password = textVlaue;
});
},obscureText: true,hintText: 'Enter Password',validator: (confirmPassword) {
if(confirmPassword.isEmpty){
return "This email field cannot be empty";
} if (confirmPassword != password) {
return "It must match password";
} return null;
},onChanged: (textVlaue) {
setState(() {
confirmPassword = textVlaue;
});
},hintText: 'Comfirm Password',Row(
children: <Widget>[CheckBox(activeColor: Colors.orange,value: _termsAgreed,onChanged: (newValue) {
setState(() {
_termsAgreed = newValue;
});
},Text('Terms & Conditions',fontSize: 16.0),)],Row(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
InkWell( onTap: (){ widget.goToHomePage(); },child: Text('CANCEL',fontSize: 20,fontWeight: FontWeight.bold),SizedBox(width: 38,InkWell(
onTap: () {
setState(() {
saveAttempted = true;
});
if(formKey.currentState.validate()) {
formKey.currentState.save();
// _createuser(email: email,password: password);
}
},child: Container(
padding: EdgeInsets.symmetric(vertical: 16.0,horizontal: 34.0),decoration: Boxdecoration(
color: Colors.white,borderRadius: BorderRadius.circular(30.0),child: Text('SAVE',style: TextStyle(color: Colors.red,],Text('Agree to Terms & Conditions',fontSize: 12),);
}
}
解决方法
检查您是否在项目中正确初始化了 firebase
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。