对 FlutterFire 的 FirebaseAuth.currentUser 文档感到困惑
Confusion on FlutterFire's FirebaseAuth.currentUser documentation
firebase_auth.dart
中的currentUser
getter是:
/// Returns the current [User] if they are currently signed-in, or `null` if
/// not.
//
/// You should not use this getter to determine the users current state,
/// instead use [authStateChanges], [idTokenChanges] or [userChanges] to
/// subscribe to updates.
User? get currentUser {
if (_delegate.currentUser != null) {
return User._(this, _delegate.currentUser!);
}
return null;
}
我想知道这是否真的意味着“您不应该使用此 getter 来 轮询 用户当前状态”。如果使用此功能获取当前状态真的是不好的做法,有人可以解释一下原因吗? currentUser
不会自动更新吗?
这不是当前用户状态,这是当前用户。
currentUser get 方法无法确定当前用户状态。
状态必须是可变的。
如果您想在用户更改时得到通知,您必须监听 authStateChanges 以确定当前用户和用户状态。
我在需要响应身份验证状态变化的代码中使用 authStateChanges
,但我在需要知道 now 的代码中使用 currentUser
当前用户是谁,或者是否有当前用户。
我建议每个人都这样做,但我们看到用户经常在他们的代码中到处使用 currentUser
,然后处理失败,其中 authStateChanges
和 StreamBuilder
会很漂亮,我想这个文档的作者想要过于明确。
如果您有更好的措辞,我相信作者会喜欢 PR。 :)
firebase_auth.dart
中的currentUser
getter是:
/// Returns the current [User] if they are currently signed-in, or `null` if
/// not.
//
/// You should not use this getter to determine the users current state,
/// instead use [authStateChanges], [idTokenChanges] or [userChanges] to
/// subscribe to updates.
User? get currentUser {
if (_delegate.currentUser != null) {
return User._(this, _delegate.currentUser!);
}
return null;
}
我想知道这是否真的意味着“您不应该使用此 getter 来 轮询 用户当前状态”。如果使用此功能获取当前状态真的是不好的做法,有人可以解释一下原因吗? currentUser
不会自动更新吗?
这不是当前用户状态,这是当前用户。
currentUser get 方法无法确定当前用户状态。
状态必须是可变的。
如果您想在用户更改时得到通知,您必须监听 authStateChanges 以确定当前用户和用户状态。
我在需要响应身份验证状态变化的代码中使用 authStateChanges
,但我在需要知道 now 的代码中使用 currentUser
当前用户是谁,或者是否有当前用户。
我建议每个人都这样做,但我们看到用户经常在他们的代码中到处使用 currentUser
,然后处理失败,其中 authStateChanges
和 StreamBuilder
会很漂亮,我想这个文档的作者想要过于明确。
如果您有更好的措辞,我相信作者会喜欢 PR。 :)