首页 > 解决方案 > 如何提高 Flutter 应用中 Firebase 身份验证的速度

问题描述

我已经构建了一个具有登录和注册功能的应用程序。我还添加了注销功能,并且当用户关闭并重新打开应用程序 firebase 时会检查用户是否注销,但问题是当我打开应用程序 firebase 时需要花费大量时间至少 8-10 秒来检查用户是否是否已经登录,那么如何提高火力基地的速度,使用户无需等待 8-10 秒。

  void initState() {
    super.initState();
    pageController = PageController();
    // Detects when user signed in
    googleSignIn.onCurrentUserChanged.listen((account) {
      handleSignIn(account);
    }, onError: (err) {
      print('Error signing in: $err');
    });
    // Reauthenticate user when app is opened
    googleSignIn.signInSilently(suppressErrors: false).then((account) {
      handleSignIn(account);
    }).catchError((err) {
      print('Error signing in: $err');
    });
  }
  handleSignIn(GoogleSignInAccount account) async {
    if (account != null) {
      await createUserInFirestore();
      setState(() {
        isAuth = true;
      });
      configurePushNotifications();
    } else {
      setState(() {
        isAuth = false;
      });
    }
  }

标签: firebaseflutterdartfirebase-authentication

解决方案


我正在使用这种方式来检查用户是否登录应用程序

Auth auth = Auth();

  await auth.getCurrentUser().then((user) async {
      runApp(MaterialApp(
        theme: _themeData(),
        home: user != null
            ? Home(
            userId: user.uid
        )
            : Login(),
        debugShowCheckedModeBanner: false,
        routes: routes,
      ));
  }).catchError((error) {
    print("Here is ERROR $error");
  });
}

告诉用户 uid 几乎不需要一两秒钟。我希望它能解决你的问题。


推荐阅读