简体   繁体   English

如何将 If 语句添加到 main.dart 文件

[英]How Add a If statement to main.dart file

I want to add an if statement function where if current user.= null it should nav to Home() and not then it should nav to Login().我想添加一个 if 语句 function,如果当前用户= null,它应该导航到 Home() 而不是然后它应该导航到 Login()。

this is the current main file这是当前的主文件

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MaterialApp(
    title: 'TaakStore',
    home: StreamBuilder(
    stream: FirebaseAuth.instance.authStateChanges(),
    builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
      if (snapshot.hasData) {
        print(snapshot);
        return Home();
      } else {
        return Login();
      }
    },
  ),
  ));
}

instead of the streambuilder i want to add a firebase auth function而不是 streambuilder 我想添加一个 firebase auth function

this这个

if(FirebaseAuth.instance.currentUser != null){
  Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Home();));
}
else{
  Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login();));
}

Please help me!!请帮我!!

You can use ternary operation like so:您可以像这样使用三元运算:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  bool loggedIn = FirebaseAuth.instance.currentUser !=null;
  runApp(MaterialApp(
    title: 'TaakStore',
    home: loggedIn ? Home(): Login(),
  ),
 );
}

I recommend you to use 'routes' in MaterialApp, I can give you an example here:我建议您在 MaterialApp 中使用“路线”,我可以在这里给您举个例子:

initialRoute: await AppRoute.getInitialRoute()

and open AppRoute and define there并打开 AppRoute 并在那里定义

static Future<String> getInitialRoute() {

    if (FirebaseAuth.instance.currentUser != null) {
      return '/home-page';
    } else {
      return '/login-page';
    }   }

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

相关问题 'firebase/main.dart':断言失败 - 'firebase/main.dart': Failed assertion 在 main.dart 中将 Future 与 MaterialApp.router 结合使用 - Use Future with MaterialApp.router in main.dart Flutter Navigator.of(context).pushReplacementNamed 抛出:“未处理的异常:Null 检查运算符用于 null 值”在 main.dart - Flutter Navigator.of(context).pushReplacementNamed throws :"Unhandled Exception: Null check operator used on a null value" on main.dart 如何从另一个 dart 文件访问值? - How to access values from another dart file? 如何使 Dart 在全球范围内检测来自不同文件的枚举扩展名? - How to make Dart detects enum extensions from different file globally? 如何在 Flutter dart 的 ListView 中添加 floatingactionbutton - How do I add floatingactionbutton in my ListView in Flutter dart 如何访问不在树中构建小部件的 dart 文件中的继承小部件? - How do I access my inherited widget in a dart file that doesn't build a widget in the tree? 如何将时间戳(使用 GETDATE())添加到我的插入语句中? - How do I add timestamp (using GETDATE()) to my insert statement? 如何将文件添加到临时目录? - How to add a file to a temporary directory? 如何将文件添加到firestore? - How to add a file to firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM