firebase 我如何为下一个动作设置条件

bxjv4tth  于 2023-01-31  发布在  其他
关注(0)|答案(2)|浏览(88)

我想根据我是否已经登录或尚未登录来确定注销操作的条件。我只是尝试在所有内容之前使用"如果",但它显示"预期标识符"和"预期)"。我想了解这部分
我尝试在弹出菜单前使用"If"条件。
应用程序栏:应用程序栏(

title: Text("Categoria"),
            backgroundColor: Color.fromARGB(150, 255, 0, 0),

              if (FirebaseAuth.instance.signInWithEmailAndPassword(email: "usu" password: "cla"))
                {
                actions: [

                PopupMenuButton(

                onSelected: (Menu item) {
                setState(() {
                if (item == Menu.logout) {
                FirebaseAuth.instance.signOut();
                Navigator.push(context, MaterialPageRoute(
                builder: (context) => Login()));
                }
                });
                },
                itemBuilder: (BuildContext context) =>
                <PopupMenuEntry<Menu>>[
                PopupMenuItem(value: Menu.logout,
                child: Text("Cerrar sesión")
                )
                ],
                )

                ],
                }
          ),

anhgbhbe

anhgbhbe1#

您可以检查当前的身份验证状态:

FirebaseAuth.instance
  .authStateChanges()
  .listen((User? user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });
u0njafvf

u0njafvf2#

将放在PopupMenuButton行的上方

title: Text("Categoria"),
            backgroundColor: Color.fromARGB(150, 255, 0, 0),

                
                actions: [
                if (FirebaseAuth.instance.signInWithEmailAndPassword(email: "usu" password: "cla"))
                PopupMenuButton(

                onSelected: (Menu item) {
                setState(() {
                if (item == Menu.logout) {
                FirebaseAuth.instance.signOut();
                Navigator.push(context, MaterialPageRoute(
                builder: (context) => Login()));
                }
                });
                },
                itemBuilder: (BuildContext context) =>
                <PopupMenuEntry<Menu>>[
                PopupMenuItem(value: Menu.logout,
                child: Text("Cerrar sesión")
                )
                ],
                )

                ],
                
          ),

if子句只能在return部分之后使用:

return Column(
  children: [
    if(condition == true) // or you just can write condition
    Text('true')
  ]
)

或者您可以使用?和:

return Container(
  child: 
  condition == true // or you just can write condition
  ? Text('true')
  : Text('false')
)

相关问题