Android Studio 如何在Flutter项目中获取用户的显示名称而不出现空检查错误?

x9ybnkn6  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(134)

因此,目前,我正在创建一个Flutter项目,这是一个Android三轮车预订系统。我在应用程序中使用了一个抽屉小部件。我想做的是,当用户登录应用程序时,他们的名字将显示在抽屉中。然而,当我尝试按照教程视频(因为我只是一个初学者),屏幕总是变成红色,并显示错误:

"Null check operator used on a null value."

我想这是因为我试图显示用户的名字。有人知道如何修复这个错误吗?我已经被卡住了这么多天。任何帮助将不胜感激!

这是我用来获取用户名的代码片段:

UserAccountsDrawerHeader(
                decoration: BoxDecoration(
                  color: Color(0xFFFED90F),
                ),
                accountName: new Text(userModelCurrentInfo!.first_name! + " " + userModelCurrentInfo!.last_name!, // This is the line that makes it null
                style: TextStyle( color: Colors.white,
                  fontSize: 15,
                  fontFamily: "Montserrat",
                  fontWeight: FontWeight.w600,),
                ),
                accountEmail: new Text(user.email!,
                style: TextStyle( color: Colors.white,
                  fontSize: 15,
                  fontFamily: "Montserrat",
                  fontWeight: FontWeight.w600,),
                    ),
                currentAccountPicture: new CircleAvatar(
                  radius: 50.0,
                  backgroundImage: AssetImage("assets/images/machu.jpg"),
                ),
              ),

这是阅读当前用户详细信息的代码:

static void readCurrentOnlineUserInfo() async
  {

    final FirebaseAuth fAuth = FirebaseAuth.instance;
    User? currentFirebaseUser;

    currentFirebaseUser = fAuth.currentUser;

    DatabaseReference userRef = FirebaseDatabase.instance
        .ref()
        .child("passengers")
        .child(currentFirebaseUser!.uid);

    userRef.once().then((snap)
    {
      if(snap.snapshot.value != null)
      {
        userModelCurrentInfo = UserModel.fromSnapshot(snap.snapshot);
        print("name" + userModelCurrentInfo!.first_name.toString());
        print("id" + userModelCurrentInfo!.id.toString());
      }
    });
  }

**我注意到的另一件事是:**在登录时,它会显示空错误,然后当我转到另一个选项卡并返回到显示空错误的页面时,它突然消失了。为什么呢?

zaq34kh6

zaq34kh61#

试试这个。添加了非空条件

UserAccountsDrawerHeader(
                decoration: BoxDecoration(
                  color: Color(0xFFFED90F),
                ),
accountName: userModelCurrentInfo!.first_name !=null && userModelCurrentInfo!.last_name !=null ?
                new Text(userModelCurrentInfo!.first_name! + " " + userModelCurrentInfo!.last_name!, 
                style: TextStyle( color: Colors.white,
                  fontSize: 15,
                  fontFamily: "Montserrat",
                  fontWeight: FontWeight.w600,),
                ):new Text('')// added null condition here, 
                accountEmail: new Text(user.email!,
                style: TextStyle( color: Colors.white,
                  fontSize: 15,
                  fontFamily: "Montserrat",
                  fontWeight: FontWeight.w600,),
                    ),
                currentAccountPicture: new CircleAvatar(
                  radius: 50.0,
                  backgroundImage: AssetImage("assets/images/machu.jpg"),
                ),
              ),

相关问题