我正在嘗試將舊的顫振代碼轉換為空安全代碼,并遇到使用 firebase 的抽象身份驗證類的問題,基本上它在監聽 authStateChange
abstract class AuthBase {
User get currentUser;
Stream<User> authStateChanges();
....
}
class Auth implements AuthBase {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
@override
Stream<User> authStateChanges() => _firebaseAuth.authStateChanges();
但是在運行代碼后它回傳A value of type 'Stream<User?>' can't be returned from the method 'authStateChanges' because it has a return type of 'Stream<User>'.錯誤所以我所做的就是強制轉換它
@override
Stream<User> authStateChanges() => _firebaseAuth.authStateChanges() as Stream<User>;
但是現在我遇到了一個新問題type '_AsBroadcastStream<User?>' is not a subtype of type 'Stream<User>' in type cast,任何關于如何解決這個問題的建議。
uj5u.com熱心網友回復:
_firebaseAuth.authStateChanges()
將給 User 或 null 所以需要使它可以為空
Stream<User?> authStateChanges() => _firebaseAuth.authStateChanges() as Stream<User?>;
并且還需要在基類中進行更改以及相同的更改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441453.html
上一篇:Dart建構式給了我空值
下一篇:如何追加到1個資料
