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.