gpt4 book ai didi

flutter - getter 'email'在null-Flutter/Firebase上被调用

转载 作者:行者123 更新时间:2023-12-03 03:33:45 25 4
gpt4 key购买 nike

我是新手,需要一小段代码的帮助。虽然我的代码有效,但是它首先显示错误/红色屏幕(毫秒)。当我以这种方式收到登录用户的电子邮件:this._user?.email,时,它显示:

A non-null String must be provided to a Text widget. Failed assertion: 'data != null'



当我以这种方式收到电子邮件时: _user.email,显示:

The getter 'email' was called on null.

Receiver: null

Tried calling: email



请提出如何引发和捕获此异常或解决此问题的建议。任何帮助将不胜感激,欢迎提出意见。我的完整代码在这里:
class MyAccountSetting extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: AccountSetting(),
);
}
}

class AccountSetting extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new AccountSettings();
}
}

class AccountSettings extends State<AccountSetting> {
final FirebaseAuth _auth = FirebaseAuth.instance;
FirebaseUser _user;

@override
void initState() {
super.initState();
initUser();
}
initUser() async {
_user = await _auth.currentUser();
setState(() {});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
margin: const EdgeInsets.only(top: 10.0),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
IconButton(
icon: Icon(Icons.mail),
iconSize: 30,
onPressed: () {},
),
Text(
_user.email,
// this._user?.email,
style: TextStyle(fontSize: 18),
),
],
),
]
),
),
);
}
}

最佳答案

您在等待检索用户时要显示什么?例如,如果您想显示一个空字符串(''),则可以编写如下内容:

_user == null ? '' : _user.email

甚至更短:

_user?.email ?? ''

这样,如果 _user为null,则将显示一个空字符串。

关于flutter - getter 'email'在null-Flutter/Firebase上被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62243904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com