英文:
Null safety Problem while getting current user data using FirebaseAuth class in Flutter
问题
以下是您要的代码翻译部分:
我是Flutter的新手,正在尝试学习Firebase。在下面的代码中,我尝试获取已登录用户的数据。但我得到了**_CastError (Null check operator used on a null value)**。我尝试解决它,但未能完成。我该如何解决它?
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
final user = FirebaseAuth.instance.currentUser!;
var useremail = "Error";
if (user.email != null) { //********问题出现在这里**********
var useremail = user.email;
}
print("$user");
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("HomeScreen"),
],
)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(useremail),
ElevatedButton(
onPressed: () {
FirebaseAuth.instance.signOut();
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => MyHomePage(),
));
},
child: Text("Sign Out")),
],
),
));
}
}
英文:
I am new in Flutter and trying to learn Firebase. In the given below code,I am trying to get data of currentUser that is logged in. But I get _CastError (Null check operator used on a null value). I tried to solve it but couldn't accomplished it. How can I solve it?
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
final user = FirebaseAuth.instance.currentUser!;
var useremail = "Error";
if (user.email != null) { //********Problem occurs here**********
var useremail = user.email;
}
print("$user");
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("HomeScreen"),
],
)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(useremail),
ElevatedButton(
onPressed: () {
FirebaseAuth.instance.signOut();
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => MyHomePage(),
));
},
child: Text("Sign Out")),
],
),
));
}
}
```
</details>
# 答案1
**得分**: 2
将上面的代码块替换为以下内容:
```dart
final User? user = FirebaseAuth.instance.currentUser;
var useremail = "Error";
if (user?.email != null) {
useremail = user!.email!;
}
```
问题在于你在`FirebaseAuth.instance.currentUser`后面加了一个感叹号,表示用户不能为null。
此外,在注销用户之前不要忘记检查用户是否为null。
要了解更多关于空安全的信息,请访问:https://dart.dev/null-safety
<details>
<summary>英文:</summary>
Replace the above block of code by this:
final User? user = FirebaseAuth.instance.currentUser;
var useremail = "Error";
if (user?.email != null) {
useremail = user!.email!;
}
The problem is that you gave an exclamation after FirebaseAuth.instance.currentUser, that stated that the user can't be null.
Also, don't forget to check if user is null or not before signing him out.
To learn more about null safety, visit: https://dart.dev/null-safety
</details>
# 答案2
**得分**: 0
除了检查 user.email != null 外,还要验证 user 对象不为 null。
<details>
<summary>英文:</summary>
In addition to checking that user.email != null , also verify that the user object is not null.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论