【发布时间】: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