Flutter 如何检查用户是否在Flatter Firebase中登录

Flutter 如何检查用户是否在Flatter Firebase中登录,flutter,firebase-authentication,flutter-layout,Flutter,Firebase Authentication,Flutter Layout,大家好。我是新手,我想检查用户是否已登录。如果是这样,用户将导航到主屏幕 这是我的主飞镖 void main() async{ runApp(MyApp()); WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); } // ignore: must_be_immutable class MyApp extends StatelessWidget { String initRout

大家好。我是新手,我想检查用户是否已登录。如果是这样,用户将导航到主屏幕

这是我的主飞镖

void main() async{
  runApp(MyApp());
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
}

// ignore: must_be_immutable
class MyApp extends StatelessWidget {
  
String initRoute;
User user = FirebaseAuth.instance.currentUser;

getUser(){
  
  if (user != null) {
  initRoute = MainScreen.routeName;
} else {
  initRoute = SplashScreen.routeName;
}
}
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Instant Tasker',
      theme: theme(),
      initialRoute: initRoute,
      routes: routes,
    );
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp().then((value) => print(value));
  runApp(MyApp());
}


确定用户是否已登录的最简单、最好的方法是使用共享首选项

只需在pubspec.yaml文件中添加依赖项

每次用户登录特定设备时,您都可以使用共享首选项将某些值保存为手机存储器中的键值对

下次每当用户打开应用程序时,您只需检查共享首选项中是否有可用的值。如果是,则打开主屏幕,否则打开启动屏幕

这是迄今为止最干净的解决方案

您可以阅读有关共享首选项的更多信息

如果您仍然需要一个示例代码。请发表评论,我将编辑答案


谢谢

如果用户登录,您应该使用包含mainScreen返回语句的小部件的返回语句对函数getuser进行右键操作,或者在splashscreen中进行右键操作,并在home中使用此函数:

 Widget  getUser(){

 if (user != null) {
 mainScreen();
 } else {
 splashScreen();
 }
 }
 @override
 Widget build(BuildContext context) {
 return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'Instant Tasker',
  theme: theme(),
  home:getUser(),
 );
  }
 }

我看到的正确方法是通过Firebase插件,下面是它的外观

主要是飞镖

void main() async{
  runApp(MyApp());
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
}

// ignore: must_be_immutable
class MyApp extends StatelessWidget {
  
String initRoute;
User user = FirebaseAuth.instance.currentUser;

getUser(){
  
  if (user != null) {
  initRoute = MainScreen.routeName;
} else {
  initRoute = SplashScreen.routeName;
}
}
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Instant Tasker',
      theme: theme(),
      initialRoute: initRoute,
      routes: routes,
    );
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp().then((value) => print(value));
  runApp(MyApp());
}


如果用户登录,它将返回uid(字符串),否则为空

谢谢各位,我解决了我的问题。stackoverflow上的每个人都提供了复杂的解决方案,但仍然没有解决问题。每个人都犯的错误是,他们在“FirebaseApp初始化”之前使用了Firebase。因此,我只需在FirebaseApp初始化时调用runApp。cHere是我的代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp().then((value) {
    runApp(MyApp());
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    User user = FirebaseAuth.instance.currentUser;
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Instant Tasker',
      theme: theme(),
      initialRoute: user != null ? MainScreen.routeName : SplashScreen.routeName,
      routes: routes,
    );
  }
}

你能添加更多的代码吗。在这里,我检查了您的登录过程。在检查了登录代码后,我将能够解决您的问题。这是main.dart的完整代码。您在哪里找到getUSer()func?你从哪里进口的??我需要看看,否则我就帮不上忙了我没有导入。getUser()函数就是在这里创建的。我建议大家看看这个youtube播放列表,了解更多关于在Flatter中使用firebase的信息:我使用了stackoverflow帖子中提到的共享prefences,但它移到了闪屏上,结果什么都没用,甚至按钮都没用。您能解释一下我应该如何使用它吗?它显示没有创建Firebase应用程序“[默认]”-请调用Firebase.initializeApp()。。。。。。。但是我在主“E/flatter(27826)”中使用了Firebase.initializeApp():[ERROR:flatter/lib/ui/ui\u dart\u state.cc(177)]未处理的异常:[core/no app]没有创建Firebase app'[DEFAULT]'-调用Firebase.initializeApp()``你需要调用Firebase.initializeApp()检查更新的答案哈哈哈,你刚才复制了我的解决方案。最好努力工作