0
votes

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.

Anonymous Login Code

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

Main Code

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,
          );
        }),
      ),
    )
  );
}

Main Code

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

Log Of Main Code

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

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

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"?

SOLVE

Problem solved with wiping data and using new emulator.

1

1 Answers

1
votes

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.

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!');
    }
  });