问题描述
我正在使用Firebase,并在单击注册"按钮时不断收到此烦人的消息:
I am using Firebase and keep getting this annoying message when I click on the Sign Up button :
代码工作正常.该应用程序已构建并成功运行.输入我的电话,电子邮件和密码并单击注册按钮时,就会发生这种情况.我的代码是:
The code works fine. The app is built and run successfully. This happens when I enter my phone, email and password and click on the signup button. My code is:
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;
bool willLogin = true;
bool showPassword = false;
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);
}
}
void _signup() async {
_formKey.currentState.validate();
try {
final newUser = await _auth.createUserWithEmailAndPassword(
email: _email.text, password: _password.text);
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;
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
_signup();
}
});
},
),
],
),
],
),
),
);
}
}
推荐答案
问题在于,这里:
onPressed: () {
setState(() {
willLogin = !willLogin;
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
_signup();
}
});
},
单击此按钮,您正在使用 setState
并将 willLogin
分配为false,因此它甚至没有调用 _signup()
因为它会立即调用 build()
方法.因此,现在您有了 willLogin = false
,它将在此小部件的屏幕上显示给您:
on click of this button, you are using setState
and assign willLogin
equal to false, therefore it is not even calling _signup()
since it is immediately calling the build()
method. So now you have willLogin = false
, which is showing you this in the screen this widget:
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();
}
},
),
根据您的代码,如果willLogin为false,则显示 Signing
文本并调用 _login()
方法.由于您正在调用 _login()
方法进行注册,因此您会收到此错误,因为用户甚至没有在Firebase身份验证中注册.
Here according to your code if willLogin is false then show Sign up
text and call _login()
method. Since you are calling _login()
method for sign up you are getting this error since the user is not even registered in firebase authentication.
您需要做的就是更改逻辑,或者删除 setState
,这样将调用 _signup()
方法,或者保持原样但要更改 _login()
到 _signup()
.
What you need to do is change the logic, either remove setState
, this way _signup()
method will be called, or keep it as it is but change _login()
to _signup()
.
请尝试以下操作:
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();
willLogin ? _login() : _signup();
}
},
),
这篇关于Flutter断言错误:flutter:'package:firebase_auth_platform_interface/src/method_channel/method_channel_user_credential.dart':断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!