【问题标题】:flutter: 'package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 487 pos 12: 'email != null': is not true颤振:'package:firebase_auth/src/firebase_auth.dart':断言失败:第 487 行 pos 12:'email!= null':不正确
【发布时间】:2021-03-27 21:29:18
【问题描述】:

我正在使用 Firebase,当我点击登录按钮时不断收到这条烦人的消息:

flutter:'package:firebase_auth/src/firebase_auth.dart':断言失败:第 487 行 pos 12:'email != null':不正确。

代码运行良好。该应用程序已构建并成功运行。当我输入我的电子邮件和密码并单击登录按钮时,就会发生这种情况。参考这些线程:https://github.com/flutter/flutter/issues/22028_AssertionError ('package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 95 pos 12: 'email != null': is not true.),但没有任何帮助。任何帮助将不胜感激:)

我的代码是:

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

class Auth extends StatefulWidget {
  @override
  _AuthState createState() => _AuthState();
}

class _AuthState extends State<Auth> {
  final _formKey = GlobalKey<FormState>();
  final _password = TextEditingController(),
      _email = TextEditingController(),
      _phone = TextEditingController();
  final _auth = FirebaseAuth.instance;
  String email;
  String password;

  bool willLogin = true;
  bool showPassword = false;
  void _login() async {
    _formKey.currentState.validate();
    try {
      final existingUser = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      if (existingUser != null) {
        Navigator.pushNamed(context, '/home');
      }
    } catch (e) {
      print(e);
    }
  }

  void _signup() async {
    _formKey.currentState.validate();
    try {
      final newUser = await _auth.createUserWithEmailAndPassword(
          email: email, password: password);
      if (newUser != null) {
        Navigator.pushNamed(context, '/home');
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dog SOS"),
        leading: Icon(Icons.pets),
      ),
      body: Form(
        key: _formKey,
        child: ListView(
          padding: const EdgeInsets.all(24),
          children: [
            const SizedBox(
              height: 10,
            ),
            Center(
              child: CircleAvatar(
                backgroundColor: Theme.of(context).primaryColor,
                child: Icon(
                  Icons.shield,
                  color: Colors.white,
                  size: 50,
                ),
                radius: 60,
              ),
            ),
            const SizedBox(
              height: 20,
            ),
            if (!willLogin) ...[
              TextFormField(
                decoration: InputDecoration(
                  labelText: "Phone",
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
                keyboardType: TextInputType.phone,
                textInputAction: TextInputAction.next,
                controller: _phone,
                validator: (value) =>
                    value.isEmpty ? "Please Enter Phone Number" : null,
              ),
              const SizedBox(
                height: 10,
              ),
            ],
            TextFormField(
              decoration: InputDecoration(
                labelText: "Email",
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
              ),
              keyboardType: TextInputType.emailAddress,
              textInputAction: TextInputAction.next,
              controller: _email,
              validator: (value) => value.isEmpty ? "Please Enter Email" : null,
            ),
            const SizedBox(
              height: 10,
            ),
            TextFormField(
              decoration: InputDecoration(
                labelText: "Password",
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                suffixIcon: IconButton(
                  icon: Icon(
                    showPassword
                        ? Icons.visibility_off_outlined
                        : Icons.visibility_outlined,
                  ),
                  onPressed: () {
                    setState(() {
                      showPassword = !showPassword;
                    });
                  },
                ),
              ),
              obscureText: !showPassword,
              textInputAction: TextInputAction.done,
              controller: _password,
              validator: (value) =>
                  value.isEmpty ? "Please Enter Password" : null,
            ),
            const SizedBox(
              height: 10,
            ),
            RaisedButton(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
              ),
              child: Text(
                willLogin ? "\nLOGIN\n" : "\nSIGN UP\n",
                style: Theme.of(context)
                    .textTheme
                    .button
                    .copyWith(color: Colors.white),
              ),
              color: Theme.of(context).primaryColor,
              onPressed: () {
                if (_formKey.currentState.validate()) {
                  _formKey.currentState.save();
                  _login();
                }
              },
            ),
            Row(
              children: [
                Text(willLogin
                    ? "Don't have an account?"
                    : "Already have an account?"),
                FlatButton(
                  child: Text(
                    willLogin ? "Create One." : "Login.",
                    style: Theme.of(context).textTheme.button.copyWith(
                          color: Theme.of(context).primaryColor,
                        ),
                  ),
                  onPressed: () {
                    setState(() {
                      willLogin = !willLogin;
                      _signup();
                    });
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 在您尝试使用它时听起来像 email == null。
  • @DougStevenson,我该如何解决?你能帮我修改一下代码吗?

标签: firebase flutter dart firebase-authentication


【解决方案1】:
  String email;
  String password;

这两个都是空的,它们没有引用任何东西。您必须执行以下操作,首先将控制器绑定到您已经在执行的字段:

 void _login() async {
    _formKey.currentState.validate();
    try {
      final existingUser = await _auth.signInWithEmailAndPassword(
          email: _email.text, password: _password.text);
      if (existingUser != null) {
        Navigator.pushNamed(context, '/home');
      }
    } catch (e) {
      print(e);
    }
  }

使用TextEditingController类的属性text获取_email_password的值。

【讨论】:

  • 非常感谢彼得。像魅力一样工作!
  • 是的,我正在尝试,但堆栈溢出似乎不喜欢速度,它要求我再等 2 分钟以将其标记为正确哈哈:) 再次感谢。
  • 嗨@Peter Haddad 很抱歉再次打扰您,您提到的解决方案在登录时效果很好。我有另一个按钮(创建一个),它将用于创建一个新帐户。单击该按钮时,我在运行时收到此异常:PlatformException (PlatformException(weak-password, Password should be at least 6 characters, {code: weak-password, message: Password should be at least 6 characters, nativeErrorMessage: The password must be 6 characters long or more., nativeErrorCode: 17026, additionalData: {}}, null))
  • 密码长度超过6个字符
  • 我明白了。但是当我按下“创建一个”按钮时,它会显示这一点。它不会带我进入我的注册功能
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
  • 2021-03-04
  • 2020-03-01
  • 2022-06-27
相关资源
最近更新 更多