Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 未处理的异常:失败的断言:第183行位置12:';电子邮件!=空';:事实并非如此_Flutter_Firebase Authentication - Fatal编程技术网

Flutter 未处理的异常:失败的断言:第183行位置12:';电子邮件!=空';:事实并非如此

Flutter 未处理的异常:失败的断言:第183行位置12:';电子邮件!=空';:事实并非如此,flutter,firebase-authentication,Flutter,Firebase Authentication,我正在使用Flatter使用Firebase身份验证制作注册表,但当我尝试处理身份验证时,此消息会一直显示,同时尝试使用Scaffold.of(context.showSnackBar)向用户显示注册错误消息。showSnackBar SnackBar(content:Text(e.message)at Sign-up小部件 class SignupScreen extends StatelessWidget { static String id = '/SignupScreen'; Glo

我正在使用Flatter使用Firebase身份验证制作注册表,但当我尝试处理身份验证时,此消息会一直显示,同时尝试使用
Scaffold.of(context.showSnackBar)向用户显示注册错误消息。showSnackBar SnackBar(content:Text(e.message)
at Sign-up小部件

  class SignupScreen extends StatelessWidget {
static String id = '/SignupScreen';
GlobalKey<FormState> _globalKey = GlobalKey<FormState>();
String _email, _password, _name;
final _auth = Auth();
@override
Widget build(BuildContext context) {
return Scaffold(Textfield(
          hint: 'Enter your name',
          icon: Icons.person,
          savingData: (value) {
            _name = value;
          },
        ),
        SizedBox(
          height: height * .02,
        ),
        Textfield(
          hint: 'Enter your e-mail',
          icon: Icons.email,
          savingData: (value) {
            _email =value;
          },
        ),
        SizedBox(
          height: height * .02,
        ),
        Textfield(
          hint: 'Enter your Password',
          icon: Icons.lock,
          savingData: (value) {
            _password = value;
          },
        ),
        SizedBox(
          height: height * .05,
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 120.0),
          child: Builder(
            builder: (context) => RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20.0)),
                child: Text(
                  'Register',
                  style: TextStyle(color: Colors.white),
                ),
                color: Colors.black,
                onPressed: () async {
                  if (_globalKey.currentState.validate()) {
                    try {
                        await _auth.SignupClass(_email, _password);
                      _globalKey.currentState.save();
                    }on FirebaseException catch (e) {
                      print(e.toString());
                      Scaffold.of(context).showSnackBar(
                        SnackBar(
                          content: Text(e.message),
                        ),
                      );
                    }
                  }
                }),
          ),
        ),
     )
      }
首先,我创建了一个auth.dart文件

import 'package:firebase_auth/firebase_auth.dart';

class Auth {
final FirebaseAuth _auth = FirebaseAuth.instance;

 //Sign up method
Future SignupClass(String email, String password) async {
final UserCredential user = await _auth.createUserWithEmailAndPassword(
        email: email, password: password);
}
然后我创建了这个TextField类来模块化我的代码

class Textfield extends StatelessWidget {
final String hint;
final IconData icon;
final Function savingData;
String _emptyMessage(String str) {
switch (hint) {
  case 'Enter your name':
    return 'Name is required';
  case 'Enter your e-mail':
    return 'Email is required';
  case 'Enter your Password':
    return 'Password is required';
}
}

Textfield(
  {@required this.savingData, @required this.hint, @required this.icon});
@override
Widget build(BuildContext context) {
return Padding(
  padding: const EdgeInsets.symmetric(horizontal: 20.0),
  child: TextFormField(
    validator: (value) {
      if (value.isEmpty) {
        return _emptyMessage(hint);
      }
      return null;
    },
    onSaved: savingData
    }}
这是Sig-up小部件

  class SignupScreen extends StatelessWidget {
static String id = '/SignupScreen';
GlobalKey<FormState> _globalKey = GlobalKey<FormState>();
String _email, _password, _name;
final _auth = Auth();
@override
Widget build(BuildContext context) {
return Scaffold(Textfield(
          hint: 'Enter your name',
          icon: Icons.person,
          savingData: (value) {
            _name = value;
          },
        ),
        SizedBox(
          height: height * .02,
        ),
        Textfield(
          hint: 'Enter your e-mail',
          icon: Icons.email,
          savingData: (value) {
            _email =value;
          },
        ),
        SizedBox(
          height: height * .02,
        ),
        Textfield(
          hint: 'Enter your Password',
          icon: Icons.lock,
          savingData: (value) {
            _password = value;
          },
        ),
        SizedBox(
          height: height * .05,
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 120.0),
          child: Builder(
            builder: (context) => RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20.0)),
                child: Text(
                  'Register',
                  style: TextStyle(color: Colors.white),
                ),
                color: Colors.black,
                onPressed: () async {
                  if (_globalKey.currentState.validate()) {
                    try {
                        await _auth.SignupClass(_email, _password);
                      _globalKey.currentState.save();
                    }on FirebaseException catch (e) {
                      print(e.toString());
                      Scaffold.of(context).showSnackBar(
                        SnackBar(
                          content: Text(e.message),
                        ),
                      );
                    }
                  }
                }),
          ),
        ),
     )
      }

您需要将小部件转换为StatefulWidget,并在文本字段的onChanged方法上调用setState()。因为在调用生成之前,小部件的状态不会得到更新,所以您的_电子邮件、_密码和_名称字段将永远不会更新。这将解决您的问题,但有更好的方法可以做到这一点

有许多状态管理库在很大程度上有助于将这种状态保持在UI之外(通常,最好将任何非UI状态与UI分开)。以下是一些建议:

  • 集团
  • 提供者
  • RxDart

  • YouTube上有很多教程,你可以查看。我推荐的一些教程来自AndyJulow、ResoCoder和MarcusNg。

    太好了!!你帮了我很多…非常感谢@Scott