簡體   English   中英

Flutter FirebaseAuth:我如何檢查用戶是否在無效中登錄?

[英]Flutter FirebaseAuth: How can i check if a User is LoggedIn in a void?

我是 FirebaseAuth 的新手,我希望它檢查用戶是否已登錄,如果沒有,他將被重定向。

那是我的空白:

  void newEntry() {
    showDialog<AlertDialog>(
        context: context,
        builder: (BuildContext context) {
          return neuerEintrag(addItem);
        });
  }

這是我在另一個 class 中檢查用戶 os LoggedIn:

    class loginProfile extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final AuthService auth = Provider.of(context).auth;
    return StreamBuilder<String>(
      stream: auth.onAuthStateChanged,
      builder: (context, AsyncSnapshot<String> snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
          final bool signedIn = snapshot.hasData;
          return signedIn ? Profil() : FirstView();
        }
        return CircularProgressIndicator();
      },
    );
  }
}

謝謝!

也許使用currentUser同步 getter 就足夠了?

if (FirebaseAuth.instance.currentUser != null) {...}

參考: https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth/currentUser.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM