Flutter 颤振-Firebase auth onAuthStateChanged不显示仪表板
我对Flatter是个新手,在实现Firebase身份验证时遇到了一些问题 所以,我有一个MaterialApp小部件,我已经注册了一些路由。我有一个包装器StreamBuilder,它监听Firebase onAuthStateChanged事件,每次我有一个新的用户值时,MaterialApp都会收到这个新值 我的问题是,我无法找到将用户导航到登录屏幕(如果没有当前用户)或仪表板(如果当前用户有效)的最佳方法。你能帮我吗?我在web上找到的每个解决方案都没有实现路由,而是返回一个小部件。有没有办法通过路线来实现这一点 我的主要班级代码如下:Flutter 颤振-Firebase auth onAuthStateChanged不显示仪表板,flutter,firebase-authentication,Flutter,Firebase Authentication,我对Flatter是个新手,在实现Firebase身份验证时遇到了一些问题 所以,我有一个MaterialApp小部件,我已经注册了一些路由。我有一个包装器StreamBuilder,它监听Firebase onAuthStateChanged事件,每次我有一个新的用户值时,MaterialApp都会收到这个新值 我的问题是,我无法找到将用户导航到登录屏幕(如果没有当前用户)或仪表板(如果当前用户有效)的最佳方法。你能帮我吗?我在web上找到的每个解决方案都没有实现路由,而是返回一个小部件。有没
void main() async {
// Catch all exceptions and print them to log.
FlutterError.onError = (FlutterErrorDetails details) {
logger.e("", details.exception, details.stack);
};
// Initialize main engines.
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
// Start main app.
runApp(
EasyLocalization(
supportedLocales: [
Locale('en', 'US'),
Locale('el', 'GR')
],
path: 'assets/translations',
fallbackLocale: Locale('en', 'US'),
useFallbackTranslations: true,
child: App(),
),
);
}
class App extends StatelessWidget {
// Future to initialize firebase.
final Future<FirebaseApp> _initialization = Firebase.initializeApp(
options: FirebaseOptions(
apiKey: "*****************",
authDomain: "*****************",
projectId: "*****************",
storageBucket: "*****************",
messagingSenderId: "*****************",
appId: "*****************",
measurementId: "*****************")
);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _initialization,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
logger.e("Error while retrieving firebase credentials.", snapshot.error);
return CircularProgressIndicator();
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return StreamBuilder<User?>(
stream: Authentication.onAuthStateChanged,
builder: (context, snapshot) {
// Extract user from snapshot.
final User? user = snapshot.data;
return StreamProvider<TreatUser?>(
create: (_) => TreatUser.streamUser(context, user?.uid),
initialData: null,
child: MaterialApp(
title: "[Dashboard]",
theme: Themes.mainTheme(context),
darkTheme: Themes.mainThemeDark(context),
themeMode: ThemeMode.light,
debugShowCheckedModeBanner: true,
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
routes: {
AppRoutes.login: (context) => LoginScreen(),
AppRoutes.passReset: (context) => ResetPassScreen(),
AppRoutes.dashboard: (context) => DashboardScreen(),
AppRoutes.user_profile: (context) => UserProfile(),
AppRoutes.items: (context) => ItemsScreen(),
},
initialRoute: user != null ? AppRoutes.dashboard : AppRoutes.login,
),
);
},
);
}
logger.d("Firebase not connected yet. Waiting.");
// Otherwise, show something whilst waiting for initialization to complete
return CircularProgressIndicator();
},
);
}
}
void main()异步{
//捕获所有异常并将其打印到日志。
flatterError.onError=(flatterErrorDetails){
logger.e(“,details.exception,details.stack);
};
//初始化主引擎。
WidgetsFlutterBinding.ensureInitialized();
等待EasyLocalization.ensureInitialized();
//启动主应用程序。
runApp(
易本地化(
支持的区域设置:[
地区('en','US'),
语言环境('el','GR')
],
路径:“资产/翻译”,
后备语言环境:语言环境('en','US'),
useFallbackTranslations:正确,
子:App(),
),
);
}
类应用程序扩展了无状态小部件{
//Future将初始化firebase。
最终未来_初始化=Firebase.initializeApp(
选项:FirebaseOptions(
apiKey:“****************”,
authDomain:“*******************”,
项目D:“****************”,
storageBucket:“****************”,
messagingSenderId:“*******************”,
appId:“****************”,
测量编号:“****************”)
);
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:_初始化,
生成器:(上下文,快照){
//检查错误
if(snapshot.hasError){
logger.e(“检索firebase凭据时出错。”,snapshot.Error);
返回循环ProgressIndicator();
}
//完成后,显示您的应用程序
if(snapshot.connectionState==connectionState.done){
返回流生成器(
流:Authentication.onAuthStateChanged,
生成器:(上下文,快照){
//从快照中提取用户。
最终用户?用户=snapshot.data;
返回流提供程序(
创建:()=>TreatUser.streamUser(上下文,用户?.uid),
initialData:null,
孩子:MaterialApp(
标题:“[仪表板]”,
主题:主题。主主题(上下文),
黑暗主题:主题。主要主题(背景),
themeMode:themeMode.light,
debugShowCheckedModeBanner:true,
localizationsDelegates:context.localizationDelegates,
supportedLocales:context.supportedLocales,
locale:context.locale,
路线:{
AppRoutes.login:(上下文)=>LoginScreen(),
AppRoutes.passReset:(上下文)=>ResetPassScreen(),
AppRoutes.dashboard:(上下文)=>DashboardScreen(),
AppRoutes.user_profile:(上下文)=>UserProfile(),
AppRoutes.items:(上下文)=>ItemsScreen(),
},
initialRoute:user!=null?AppRoutes.dashboard:AppRoutes.login,
),
);
},
);
}
logger.d(“Firebase尚未连接。正在等待”);
//否则,在等待初始化完成时显示某些内容
返回循环ProgressIndicator();
},
);
}
}
提前感谢您的帮助和时间 当Material应用程序下有多个支架时(在验证过程之前),会发生这种情况。因此authStateChanges被另一个替换,并且没有响应 解决方案是在initialRoute或home参数中使用FirebaseAuth.currentUser 登录后,手动推动仪表板并删除所有以前的屏幕 同样,您也可以在注销时执行此操作