我想添加一个if语句函数,如果当前用户!= null,它应该导航到Home(),而不是,那么它应该导航到Login()。
这是当前的主文件
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();
}
},
),
));
}我想添加一个firebase auth函数,而不是streambuilder
这
if(FirebaseAuth.instance.currentUser != null){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Home();));
}
else{
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login();));
}请帮帮我!!
发布于 2021-01-14 22:15:46
你可以像这样使用三元运算:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
bool loggedIn = FirebaseAuth.instance.currentUser !=null;
runApp(MaterialApp(
title: 'TaakStore',
home: loggedIn ? Home(): Login(),
),
);
}https://stackoverflow.com/questions/65720501
复制相似问题