当FirebaseAuth示例启动时,它在Flutter表单上给出错误

slmsl1lt  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(202)

这是我的第一个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),),
            SizedBox(height: 5,),
            TextFormField( autovalidate: saveAttempted, 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,
              decoration: InputDecoration(errorStyle: TextStyle(color: Colors.white), hintText: 'Enter Password', hintStyle: TextStyle(color: Colors.white.withOpacity(0.6)), border: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.white),
              )), style: TextStyle(fontSize: 22, color: Colors.white),),
            SizedBox(height: 5,),
            TextFormField( autovalidate: saveAttempted, 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;
              });
              },
              obscureText: true,
              decoration: InputDecoration(errorStyle: TextStyle(color: Colors.white), hintText: 'Comfirm Password', hintStyle: TextStyle(color: Colors.white.withOpacity(0.6)), border: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.white),
              )), style: TextStyle(fontSize: 22, color: Colors.white),),
            SizedBox(height: 5,),
            Row(
              children: <Widget>[Checkbox(activeColor: Colors.orange, value: _termsAgreed, onChanged: (newValue) {
                setState(() {
                  _termsAgreed = newValue;
                });
              },), Text('Terms & Conditions', style: TextStyle(color: Colors.white, fontSize: 16.0),)],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                InkWell( onTap: (){ widget.goToHomePage(); },
                    child: Text('CANCEL', style: TextStyle(color: Colors.white, 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, fontSize: 20, fontWeight: FontWeight.bold),)),
                ),
              ],
            ),
            Text('Agree to Terms & Conditions', style: TextStyle(color: Colors.white, fontSize: 12),),
          ],
        ),
      ),
    );
  }
}
ccrfmcuu

ccrfmcuu1#

检查您是否已在项目中正确初始化了FireBase

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

相关问题