如果您打算在整个应用程序中使用FirebaseUser或“登录用户”,我建议您在应用程序的最高级别上添加提供程序。例
void main() { runApp(MyApp()); }class MyApp extends StatelessWidget { MyApp(); @override Widget build(BuildContext context) { return MultiProvider( providers: [ StreamProvider<FirebaseUser>.value( stream: FirebaseAuth.instance.onAuthStateChanged, // Provider here ), ], child: MaterialApp( title: 'My App', debugShowCheckedModeBanner: false, theme: ThemeData( primaryColor: Colors.green, primarySwatch: Colors.green, accentColor: Colors.yellow, ), home: MainPage(), ), ); }}class MainPage extends StatefulWidget { MainPage({Key key, this.storage}) : super(key: key); final FirebaseStorage storage; @override _MainPageState createState() => _MainPageState();}class _MainPageState extends State<MainPage> with SingleTickerProviderStateMixin { @override Widget build(BuildContext context) { final user = Provider.of<FirebaseUser>(context); // gets the firebase user bool loggedIn = user != null; return loggedIn ? HomePage() : LoginPage(); // Will check if the user is logged in. And will change anywhere in the app if the user logs in }}
Fireship 185提供者
很棒的YouTube视频解释了代码
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)