简体   繁体   中英

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

I am new in FirebaseAuth and i want, that in a void it check if the User is LoggedIn and if not, that he will be redirected.

That is my void:

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

And this is my check if the User os LoggedIn, in an other class:

    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();
      },
    );
  }
}

Thanks!

Maybe using the currentUser synchronous getter would be enough?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM