[英]Unable to navigate based on auth state in flutter-firebase
I am using firebase as a backend service in my android app.I am trying to navigate user to the login screen if it is not logged in.I am checking auth state in main.dart file. 我在我的android应用中使用firebase作为后端服务。如果未登录,我试图将用户导航到登录屏幕。我正在main.dart文件中检查身份验证状态。 when app launches I am getting something in my logcat window like: 当应用启动时,我在logcat窗口中看到一些信息:
E/FirebaseInstanceId(10239): Failed to resolve target intent service, skipping classname enforcement
E/FirebaseInstanceId(10239): Error while delivering the message: ServiceIntent not found.
Below is my code: 下面是我的代码:
main.dart main.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import './home.dart';
import './orders.dart';
import './account.dart';
import './login.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyTabs(),
debugShowCheckedModeBanner: false,
routes: <String,WidgetBuilder>{
'/login':(BuildContext context) => Login()
},
theme: ThemeData(
primaryColor: Colors.white,
primaryColorDark: Colors.grey,
accentColor: Colors.green
),
);
}
}
class MyTabs extends StatefulWidget {
@override
_MyTabsState createState() => _MyTabsState();
}
class _MyTabsState extends State<MyTabs> {
FirebaseAuth auth = FirebaseAuth.instance;
int selectedIndex = 0;
final pages = [Home(),Orders(),Account()];
void choosePage(int index){
setState(() {
selectedIndex = index;
});
}
@override
void initState() {
super.initState();
if(auth.currentUser() == null){
Navigator.of(context).pushReplacementNamed("/login");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Tiffino")
),
body: pages[selectedIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: selectedIndex,
fixedColor: Colors.black,
onTap: choosePage,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("Home")
),
BottomNavigationBarItem(
icon: Icon(Icons.list),
title: Text("Orders")
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text("Account")
)
]
)
);
}
}
Someone please correct me if I did anything wrong in above code. 如果我在上述代码中做错了任何人,请纠正我。
THANKS 谢谢
The auth.currentUser()
method returns a Future, which means you should use a then()
method to resolve it or the await
operator in an async method, instead of instantly checking if it's null.## Heading ## auth.currentUser()
方法返回一个Future,这意味着您应该使用then()
方法或异步方法中的await
运算符来解析它,而不是立即检查它是否为空。
Try this with async calls, instead of using then and catchError, which would lead to a few more methods to be called: 通过异步调用尝试此操作,而不是使用then和catchError,这将导致调用更多方法:
@override
void initState() {
super.initState();
_getCurrentUser();
}
/// Checks if the user is logged in
_getCurrentUser() async {
//Notice here the await operator, instead of using then() etc.
FirebaseUser mCurrentUser = await auth.currentUser();
if(mCurrentUser != null){
authSuccess(mCurrentUser);
} else {
// not logged in
Navigator.of(context).pushReplacementNamed("/login");
}
}
void authSuccess(FirebaseUser user){
// User is logged in, do something if needed
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.