_AssertionError('package:firebase_auth/src/firebase_auth.dart':断言失败:第95行pos 12:'email!= null':不正确.) [英] _AssertionError ('package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 95 pos 12: 'email != null': is not true.)

查看:39
本文介绍了_AssertionError('package:firebase_auth/src/firebase_auth.dart':断言失败:第95行pos 12:'email!= null':不正确.)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在Firebase的基础上建立一个注册.但是,当我按下包含注册功能的按钮时,应用程序停止并且收到此错误:

My goal is to build a registration in flutter with Firebase. However, when I press the button that contains the register function, the app stops and I receive this error:

发生异常._AssertionError('package:firebase_auth/src/firebase_auth.dart':断言失败:第95行pos 12:'email!= null':不正确.

Exception has occurred. _AssertionError ('package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 95 pos 12: 'email != null': is not true.)

与firebase的集成正在工作,可能问题出在没有获取数据的textformfield上...

The integration with firebase is working, probably the problem is on textformfield that is not getting data...

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:cruke_app/main.dart';
import 'package:cruke_app/ui/login.dart';

class CrukeRegister extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Register(),
    );
  }
}

class Register extends StatefulWidget {
  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  String _email, _password, _passwordConfirm, _displayName;
  final formKey = new GlobalKey<FormState>();
  bool _autoValidate = false;
  final FirebaseAuth auth = FirebaseAuth.instance;
  bool _loading = false;
  final scaffoldKey = new GlobalKey<ScaffoldState>();

 @override
  void initState() {

    super.initState();
  } 

  Widget buildTextField(double width, String text, IconData icon, bool obscureText, String isEmpty, String _saved, bool _autoValidate){

    return Container(
      margin: EdgeInsets.only(bottom: 15),
      width: width * 0.9,
      child: TextFormField(
        validator: (input) => input.isEmpty ? isEmpty : null,
        onSaved: (input) => _saved = input,
        autovalidate: _autoValidate,
        obscureText: obscureText,
        decoration: InputDecoration(
          suffixIcon: Padding(
            padding: EdgeInsetsDirectional.only(end: 12.0),
            child: Icon(icon, color: Colors.red),
          ), 
          hintText: text,
          hintStyle: TextStyle(color: Colors.redAccent, fontSize: 15.0),
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(30.0),
            borderSide: BorderSide(
              width: 5.0,
              color: Colors.redAccent,
            ),
          ), 
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return Scaffold(
      key: scaffoldKey,
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: Container(
                margin: EdgeInsets.only(top: height * 0.025),
                child: Image.asset("images/logo_vertical.png", width: width * 0.5),
              ),
            ),
            Form(
              key: formKey,
              autovalidate: _autoValidate,
              child: Column(
                children: <Widget>[
                  buildTextField(width, "Digite seu nome", Icons.person, false, "Por favor, digite seu nome!", _displayName, _autoValidate),
                  buildTextField(width, "Digite seu e-mail", Icons.email, false, "Por favor, digite seu e-mail!", _email, _autoValidate),
                  buildTextField(width, "Digite sua senha", Icons.lock, true, "Por favor, digite sua senha!", _password, _autoValidate),
                  buildTextField(width, "Confirme sua senha", Icons.lock, true, "Por favor, confirme sua senha!", _passwordConfirm, _autoValidate),
                  Container(
                    height: height * 0.09,
                    width: width * 0.9,
                    padding: EdgeInsets.only(top: 10.0),
                    child: RaisedButton.icon(   
                      onPressed:
                        _validateSubmitRegister,
                        //Navigator.push(
                          //context,
                          //MaterialPageRoute(builder: (context) => Home()),
                        //);

                      label: Text(
                        'Registrar',
                        style: TextStyle(fontSize: 15, color: Colors.white),               
                      ),
                      color: Colors.red,
                      icon: Icon(Icons.account_circle, color: Colors.white),
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),

                    ),
                  ),
                ],
              ),
            ),

            Container(
              margin: EdgeInsets.only(top: 15),
              child: Material(
                child: InkWell(
                  onTap: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => CrukeLogin()));
                  },
                  child: Text("Já tem uma conta? Faça Login!", style: TextStyle(fontSize: 15, color: Colors.red),),
                ),

              ),

            ),
          ],
        ),
      ),

    );
  }


  void _validateSubmitRegister() async{
    final form = formKey.currentState;
    if (formKey.currentState.validate()) {
      form.save();
      AuthResult result = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);
FirebaseUser user = result.user;
    }

  }
}

推荐答案

您可以尝试以下方式:

创建枚举:枚举数据类型{NAME,EMAIL,PASS,PASS_REPEAT}

并更改您的方法:

Widget buildTextField(double width, String text, IconData icon, bool obscureText, String isEmpty, DataType type, bool _autoValidate){

  return Container(
    margin: EdgeInsets.only(bottom: 15),
    width: width * 0.9,
    child: TextFormField(
      validator: (input) => input.isEmpty ? isEmpty : null,
      onSaved: (input) {
        switch (type) {
          case DataType.NAME:
            _displayName = input;
            break;
          case DataType.EMAIL:
            _email = input;
            break;
          case DataType.PASS:
            _password = input;
            break;
          case DataType.PASS_REPEAT:
            _passwordConfirm = input;
            break;
        }
      },
      autovalidate: _autoValidate,
      obscureText: obscureText,
      decoration: InputDecoration(
        suffixIcon: Padding(
          padding: EdgeInsetsDirectional.only(end: 12.0),
          child: Icon(icon, color: Colors.red),
        ), 
        hintText: text,
        hintStyle: TextStyle(color: Colors.redAccent, fontSize: 15.0),
        border: OutlineInputBorder(
          borderRadius: BorderRadius.circular(30.0),
          borderSide: BorderSide(
            width: 5.0,
            color: Colors.redAccent,
          ),
        ), 
      ),
    ),
  );
}

这篇关于_AssertionError('package:firebase_auth/src/firebase_auth.dart':断言失败:第95行pos 12:'email!= null':不正确.)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆