我已使用 Firebase 身份验证设置了登录页面
我在我的代码中实现了一个 _submit()
函数,它使用 signInWithEmailAndPassword()
和单独的 googleSignIn()
方法登录用户
但是当我尝试使用 _submit()
登录时出现此错误
The following ArgumentError was thrown building Home(dirty, state: _HomeState#75cb9)
这是我的 _submit()
方法
_submit() async {
final isValid = _key.currentState.validate();
if (isValid) {
_key.currentState.save();
EasyLoading.show(status: "Please Wait...\n" + "Loggin in with \n");
try {
UserCredential userCredential = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
assert(await user.getIdToken() != null);
User currentUser = auth.currentUser;
assert(user.uid == currentUser.uid);
print(userCredential);
EasyLoading.dismiss();
EasyLoading.showSuccess("Welcome back");
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Home()));
} on FirebaseAuthException catch (e) {
EasyLoading.dismiss();
if (e.code == 'user-not-found') {
Alert(
context: context,
style: AlertStyle(
backgroundColor: bgcolor,
titleStyle: TextStyle(
fontFamily: 'valorant',
fontWeight: FontWeight.bold,
color: Colors.white),
descStyle: TextStyle(
fontFamily: 'valorant', fontSize: 15, color: Colors.white),
),
type: AlertType.error,
title: "Account Was Not Found !!",
desc: "the email id entered does not exist ",
buttons: [
DialogButton(
color: Colors.red,
child: Text(
"Create Account",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontFamily: 'Valorant'),
),
onPressed: () async {
Navigator.of(context).pop();
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => SignUP()));
},
width: 200,
)
],
).show();
print('No user found for that email.');
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
}
}
}
}
用户要去的类是这样的
class Home extends StatefulWidget {
const Home({Key key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
bool isLoggedIn = true;
FirebaseAuth _auth = FirebaseAuth.instance;
User _user = FirebaseAuth.instance.currentUser;
@override
Widget build(BuildContext context) {
void signout() async {
await FirebaseAuth.instance.signOut();
setState(() {
isLoggedIn = false;
});
Timer(Duration(seconds: 2), () {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => SignIN()),
(route) => false);
});
}
@override
void initState() {
super.initState();
FirebaseAuth auth = FirebaseAuth.instance;
auth.authStateChanges().listen((user) {
if (user == null) {
print("no user Logged In");
return isLoggedIn = false;
} else {
print(user.displayName + " is signed in ");
return isLoggedIn = true;
}
});
}
return isLoggedIn
? Scaffold(
appBar: AppBar(
title: Text("HI " + _user.displayName),
),
body: Center(
child: ElevatedButton(
onPressed: signout,
child: Text("Log Out"),
),
),
)
: Scaffold(
body: Center(
child: Text("NO USER LOGGED IN"),
),
);
}
}
我得到的错误是
The relevant error-causing widget was
Home lib\Authentication\signin.dart:59
When the exception was thrown, this was the stack
#0 _StringBase.+ (dart:core-patch/string_patch.dart:272:57)
#1 _HomeState.build package:myapp/home/Home.dart:62
#2 StatefulElement.build package:flutter/…/widgets/framework.dart:4775
#3 ComponentElement.performRebuild package:flutter/…/widgets/framework.dart:4658
#4 StatefulElement.performRebuild
我有一个谷歌登录功能,可以毫无问题地将我带到主页 但是当使用电子邮件 ID 和密码时,我遇到了这个错误
我的谷歌登录方法是:
signInWithGoogle() async {
EasyLoading.show(
status: "Please Wait...", maskType: EasyLoadingMaskType.black);
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
UserCredential userCredential = await auth.signInWithCredential(credential);
user = userCredential.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
User currentUser = auth.currentUser;
assert(user.uid == currentUser.uid);
EasyLoading.dismiss();
EasyLoading.showSuccess("Welcome Back !!",
maskType: EasyLoadingMaskType.black, duration: Duration(seconds: 3));
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => Home()), (route) => false);
print(user.displayName);
print(user.email);
}
这导致
在哪里
答案 0 :(得分:0)
initState
方法和 signOut
方法应该在您的 build
方法之外。
class Home extends StatefulWidget {
const Home({Key key}) : super(key: key);
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
...
void signout() async {
...
}
@override
void initState() {
...
}
@override
Widget build(BuildContext context) {
...
}
}