匿名用户未通过Firebase代码错误检测 [英] Anonymous User not detecting by firebase code error

查看:45
本文介绍了匿名用户未通过Firebase代码错误检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加匿名用户功能并将其保存到设备中以备将来使用,但是每当我重新启动应用程序Firebase时,都不会检测到currentUser是否为Anonymous,并且在3-5天前就可以正常运行了.

I'm trying to add anonymous user functionality and save this to device for further usage, but whenever I restart my app firebase not detecting if currentUser is Anonymous and it was working like 3-5 days ago perfectly.

匿名登录代码

signInAnonymously().then((value) async {
  SharedPreferences preferences = await SharedPreferences.getInstance();
  preferences.setString(
    'name',
    FirebaseAuth.instance.currentUser.uid
  );
  preferences.setBool('anon', true);
});

主要代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
    
  await Firebase.initializeApp();
    
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var name = prefs.getString('name');
  FirebaseAuth auth = FirebaseAuth.instance;
  Algolia algolia = Application.algolia;
  print(auth.currentUser.isAnonymous); // Returns false even if i logged anonymously before
  runApp(ThemeProvider(
    saveThemesOnChange: true,
    themes: [
      AppTheme(
        id: 'white',
        data: constant.whiteTheme,
        description: 'white theme'
      ),
      AppTheme(
        id: 'dark',
        data: constant.darkTheme,
        description: 'dark theme'
      ),
    ],
    child: ThemeConsumer(
      child: Builder(
        builder: (themeContext) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: name == null
             ? OnBoardScreen(
                 algolia: algolia,
               )
             : WelcomeScreen(
                 isAnon: prefs.getBool('anon'),
                 algolia: algolia,
             ),
            theme: ThemeProvider.themeOf(themeContext).data,
          );
        }),
      ),
    )
  );
}

主要代码

FirebaseAuth auth = FirebaseAuth.instance;
 
  print(auth.currentUser.uid); //returns UID of anon user
  print(auth.currentUser.isAnonymous);//returns false all the time

主要代码日志

I/flutter (31830): ci90IxegpvMyq0vGqhtHxqcrrT52
I/flutter (31830): false

所以我可以打印我的匿名用户的UID,但是isAnonymous状态仍然返回false.

So I can print UID of my anonymous user but isAnonymous state still returns false.

注意:我知道我可以使用SharedPreferences处理用户情况,但这是什么引起的呢?在用户使用"FirebaseAuth"重新启动应用程序之前,如果用户匿名登录无法获得信息?

Note: I know that I can handle user situations with SharedPreferences, but what is causing this? And can't I get if user is logged in anonymously before restarting app with "FirebaseAuth"?

解决

清除数据并使用新的模拟器解决了问题.

Problem solved with wiping data and using new emulator.

推荐答案

重新启动应用程序时,Firebase必须检查用户是否仍然具有访问权限(例如:您可能已禁用其帐户).这是异步操作,因为它需要调用服务器,并且可能需要一些时间才能完成.

When you restart the app, Firebase has to check whether the user still has access (for example: you may have disabled their account). This is an asynchronous operation, since it requires a call to the server and may take some time to complete.

当您的代码访问 FirebaseAuth.instance.currentUser 时,此异步调用可能尚未完成-导致 currentUser 仍然为 null .

When your code accesses FirebaseAuth.instance.currentUser, this asynchronous call may not have completed yet - leading to currentUser still being null.

为确保始终响应正确的身份验证状态,请使用身份验证状态侦听器,如身份验证状态:

To be certain that you always respond to the correct authentication state, use an auth state listener as shown in the documentation on authentication state:

FirebaseAuth.instance
  .authStateChanges()
  .listen((User user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });

这篇关于匿名用户未通过Firebase代码错误检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆