简体   繁体   English

首次登录后红色 null 错误只是一秒钟

[英]After first login red null error comes just for a second

When i run the app and log-in / or already logged in before closed app => "Null check operator used on a null value" error pops up and it goes away just in a second.当我运行应用程序并登录/或在关闭应用程序之前已经登录时=>“在 null 值上使用的空检查运算符”错误弹出并在一秒钟内消失。 I guess user going to page before data came up.我猜用户会在数据出现之前翻页。 If i sign-out and log in with another acc.如果我退出并使用另一个帐户登录。 Red screen wont came up but the last users data appears just for a second then current users data comes after.红屏不会出现,但最后一个用户数据只出现一秒钟,然后是当前用户数据。 Could you please help me about this.你能帮我解决这个问题吗?

My primitiv Sign-in method我的原始登录方法

Future signIn() async {
try {
  await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: emailController.text.trim(),
      password: passwordController.text.trim(),
  );
  print('Signed In');
} on FirebaseAuthException catch (e) {
  print (e);
  Utils.showSnackBar(e.message);
}}}

User Provider用户提供者

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();
  User get getUser => _user!;
  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();}}

The code i use refresh User welcomepage level我使用的代码刷新用户欢迎页面级别

class _welcomePageState extends State<welcomePage> {

  void initState() {
    super.initState();
    addData();
  }
  addData() async{
    UserProvider _userProvider = Provider.of(context,listen: false);
    await _userProvider.refreshUser();
  }

  @override

if that codes not enough to solve i can share all could you please help me to solve this problem.如果这些代码不足以解决我可以分享所有你能帮我解决这个问题吗?

You are force-unwrapping here: User get getUser => _user;, which means when you hot-reload the app it is null and the crash appears until it is null.您在这里强制解包:用户获取 getUser => _user;,这意味着当您热重新加载应用程序时,它是 null,并且崩溃出现,直到它是 null。 you should make it optional and provide a default value so it doesn't crash.您应该将其设为可选并提供默认值,这样它就不会崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM