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

When FirebaseAuth instance is initiated it gives error on Flutter form

这是我的第一个 Flutter 项目,我有一些困惑。我已经创建了这个表单,每当我启动一个 Firebase 实例时,该表单就会消失。

如果我删除此代码 final FirebaseAuth _firebaseAuth = FirebaseAuth.instance; 它工作正常,该表单将显示,但如果取消注释,该表单将消失并且 return 此错误:

Null check operator used on a null value

代码:

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

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

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