[英]currentUser Flutter won't change state
I want two show a widget if the user is logged in and show a different one, if not.如果用户已登录,我想要两个显示一个小部件,如果没有,则显示一个不同的小部件。
This is what the snippet of the code looks like:这是代码片段的样子:
class _AccountWidgetState extends State<AccountWidget> {
bool loggedIn = false;
final _auth = FirebaseAuth.instance;
late User loggedInUser;
@override
void initState() {
getCurrentUser();
super.initState();
}
void getCurrentUser() {
try {
final user = _auth.currentUser;
if (user != null) {
loggedInUser = user;
setState(() {
loggedIn == true;
});
}
} catch (e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: loggedIn
? const Text('logged in')
: SizedBox(... ),
);
}
}
No matter what the loggedIn
boolean will be always false.不管
loggedIn
boolean 将始终为假。
What's going on here?这里发生了什么?
You are not making any assignment here.你没有在这里做任何任务。 It is a condition.
这是一个条件。
void getCurrentUser() {
try {
final user = _auth.currentUser;
if (user != null) {
loggedInUser = user;
setState(() {
// Wrong -> loggedIn == true;
loggedIn = true;
});
}
} catch (e) {
print(e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.