简体   繁体   English

Null 检查用于 null 值的运算符

[英]Null check operator used on a null value

I'm getting this error when i was tried to use different pages based on the user role.当我尝试根据用户角色使用不同的页面时出现此错误。 I don't know how should I use the?我不知道我应该如何使用? or.要么。 in the StreamBuilder or the class itself.在 StreamBuilder 或 class 本身。 i don't know it said that the userDoc is null.我不知道它说userDoc是null。

Null check operator used on a null value Null 检查用于 null 值的运算符

class Start extends StatelessWidget {
  const Start({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<User?>(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (snapshot.hasData && snapshot.data != null) {
          UserHelper.saveUser(snapshot.data!);
          return StreamBuilder<DocumentSnapshot>(
            stream: FirebaseFirestore.instance
                .collection('users')
                .doc('Tma4901vh0aXGHjbsvtp8k0fTJS2')
                .snapshots(),
            builder: (BuildContext context,
                AsyncSnapshot<DocumentSnapshot> snapshot) {
              final userDoc = snapshot.data;
              final user = userDoc!.data(); // Here is the error
              if ((user as Map<String, dynamic>)['role'] == 'admin') {
                return const Home();
              } else {
                return const UserHome();
              }
            },
          );
        } else if (snapshot.hasError) {
          return const Center(child: Text('Algo ha salido mal!'));
        } else {
          return const LoginScreen();
        }
      },
    );
  }
}

Wrap this code with if(snapshot.hasData)if(snapshot.hasData)包装这段代码

final userDoc = snapshot.data;
final user = userDoc!.data();
  if ((user as Map<String, dynamic>)['role'] == 'admin') {
      return const Home();
  } else {
      return const UserHome();
  }

Then it would be like that:那么它会是这样的:

if(snapshot.hasData){
    final userDoc = snapshot.data;
    final user = userDoc!.data();
    if ((user as Map<String, dynamic>)['role'] == 'admin') {
       return const Home();
    } else {
       return const UserHome();
    }
}else{
    return TheWidgetYouWant();
}

Try this:试试这个:

  if(snapshot.hasData){
    final userDoc = snapshot.data;
    final user = userDoc!.data();
    if ((user as Map<String, dynamic>)['role'] == 'admin') {
       return const Home();
    } else {
       return const UserHome();
    }
  } else {
    return Container();
  }

if you use?如果你使用? this will throw an error when the value is null try using ?当值为 null 尝试使用时,这将引发错误? operator which will check if value is not null then it will try to call the method after this operator and not causing any error if used with null value将检查值是否不是 null 的运算符,然后它将尝试在此运算符之后调用该方法,如果与 null 值一起使用,则不会导致任何错误

change this line改变这一行

final user = userDoc!.data(); 

to this对此

final user = userDoc?.data(); 

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

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