Flutter Firebase auth 用户在设备上不是持久的
Posted
技术标签:
【中文标题】Flutter Firebase auth 用户在设备上不是持久的【英文标题】:Flutter Firebase auth user is not persistent on the device 【发布时间】:2020-11-29 06:57:06 【问题描述】:我在我的安卓 Flutter 应用程序中使用了 Firebase 手机身份验证。登录系统正常工作。但是,每次我打开应用程序时,它都会返回 null 用户。因此,我每次打开应用程序时都必须登录。我不知道这是我的错误还是错误,但我会在这里与你们分享代码。
电话验证码:
enum PhoneAuthStatus
SignedIn,
CodeSent,
HasError,
SignedOut,
class PhoneAuth with ChangeNotifier
String verificationId;
final FirebaseAuth phoneAuth = FirebaseAuth.instance;
PhoneAuthStatus phoneAuthStatus = PhoneAuthStatus.SignedOut;
PhoneAuth.initialize()
init();
Future<void> init() async
currentUser = await phoneAuth.currentUser();
if (currentUser == null)
phoneAuthStatus = PhoneAuthStatus.SignedOut;
notifyListeners();
else
phoneAuthStatus = PhoneAuthStatus.SignedIn;
PhoneAuthStatus get status => phoneAuthStatus;
FirebaseUser get user => currentUser;
Future<void> verifyPhoneNumber(String phNum) async
phoneAuth.verifyPhoneNumber(
phoneNumber: phNum,
timeout: Duration(seconds: 90),
verificationCompleted: (AuthCredential phoneAuthCredential) async
AuthResult authResult;
try
authResult =
await phoneAuth.signInWithCredential(phoneAuthCredential);
catch (error)
phoneAuthStatus = PhoneAuthStatus.HasError;
notifyListeners();
return;
currentUser = authResult.user;
uid = currentUser.uid;
phoneAuthStatus = PhoneAuthStatus.SignedIn;
notifyListeners();
,
verificationFailed: (AuthException authException)
print(authException.message);
phoneAuthStatus = (PhoneAuthStatus.HasError);
notifyListeners();
,
codeSent: (String vId, [int forceResendingToken]) async
phoneAuthStatus = (PhoneAuthStatus.CodeSent);
notifyListeners();
verificationId = vId;
,
codeAutoRetrievalTimeout: (String vId)
verificationId = vId;
);
return;
Future<void> signInWithPhoneNumber(String code) async
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: code.toString(),
);
AuthResult authResult;
try
authResult = await phoneAuth.signInWithCredential(credential);
catch (error)
phoneAuthStatus = (PhoneAuthStatus.HasError);
notifyListeners();
return;
currentUser = authResult.user;
phoneAuthStatus = (PhoneAuthStatus.SignedIn);
notifyListeners();
return;
Future<void> signOut() async
phoneAuth.signOut();
phoneAuthStatus = (PhoneAuthStatus.SignedOut);
notifyListeners();
main.dart 代码:
class MyApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: PhoneAuth.initialize()),
],
child: MaterialApp(
title: app_name,
// routes: Routes.routes,
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
)
home: Container(
child: LocationError(currentWidget: HomePage()),
),
),
);
init() 函数应该返回 PhoneAuthStatus.SignedIn。但是,它返回 PhoneAuthStatus.SignedOut,因为 currentUser 为空。 如果我做错了什么,请帮助我并告诉我。谢谢。
颤振医生:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.0, on Microsoft Windows [Version 10.0.18363.959], locale en-US)
[!] android toolchain - develop for Android devices (Android SDK version 29.0.2)
X Android license status unknown.
Try re-installing or updating your Android SDK Manager.
See https://developer.android.com/studio/#downloads or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions.
[√] Android Studio (version 4.0)
[√] VS Code (version 1.47.3)
[!] Connected device
! No devices available
! Doctor found issues in 2 categories.
【问题讨论】:
【参考方案1】:您是否检查过将 notifyListeners()
放在 if 块之外?
currentUser = await phoneAuth.currentUser();
if (currentUser == null)
phoneAuthStatus = PhoneAuthStatus.SignedOut;
else
phoneAuthStatus = PhoneAuthStatus.SignedIn;
notifyListeners();
另一个猜测是,您在 init() 方法中调用未来方法。也许这就是为什么您总是将身份验证状态设为 signOut。
“init 方法在设计上是同步的。您可以在重写的 build 方法中返回 FutureBuilder 小部件,而不是在 init 方法中创建 FutureBuilder 小部件。感觉在这里免费查看 FutureBuilder 小部件的文档,以获取有关其工作原理的示例。”
所以,我认为最好使用未来的构建器来验证用户的存在。
【讨论】:
我已经正确使用了 notifyListeners()。我将在 FutureBuilder 中尝试并让您知道。谢谢。以上是关于Flutter Firebase auth 用户在设备上不是持久的的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter 中使用 Firebase Auth 和 Firestore
检查用户是不是登录 Flutter & firebase auth |
Flutter 使用 Cloud Firestore 和 Firebase Auth 存储用户数据
检查用户是否已登录Flutter&firebase auth |