【发布时间】:2021-05-22 13:40:47
【问题描述】:
我正在尝试添加匿名用户功能并将其保存到设备以供进一步使用,但每当我重新启动我的应用程序时,firebase 都不会检测 currentUser 是否为匿名用户,并且它在 3 到 5 天前的工作状态非常完美。
匿名登录代码
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。
注意:我知道我可以使用 SharedPreferences 处理用户情况,但这是什么原因造成的?如果用户在使用“FirebaseAuth”重新启动应用程序之前匿名登录,我不能得到吗?
解决
通过擦除数据和使用新模拟器解决了问题。
【问题讨论】:
标签: flutter firebase-authentication