gpt4 book ai didi

firebase - 如何在 Flutter 中将 onAuthStateChanged 用于 Firebase Auth?

转载 作者:IT王子 更新时间:2023-10-29 07:14:00 24 4
gpt4 key购买 nike

  1. 我正在使用 firebase auth 和 google 登录来处理身份验证。
  2. 截至目前,我可以使用 firebase_auth/google_sign_in 登录我的 flutter 应用。
  3. 我使用的 firebase_auth.dart 插件来自:https://pub.dartlang.org/packages/firebase_auth
  4. 我正在使用 onAuthStateChanged 来检测用户何时登录,一切正常。
  5. 我的问题是:当我注销时,onAuthStateChanged 似乎没有注意到

这是我的代码(现在“应用程序”只是一些虚拟页面)

import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:flutter/foundation.dart';

// ************** Begin Auth

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = new GoogleSignIn();

Future<FirebaseUser> signInWithGoogle() async {
// Attempt to get the currently authenticated user
GoogleSignInAccount currentUser = _googleSignIn.currentUser;
if (currentUser == null) {
// Attempt to sign in without user interaction
currentUser = await _googleSignIn.signInSilently();
}
if (currentUser == null) {
// Force the user to interactively sign in
currentUser = await _googleSignIn.signIn();
}

final GoogleSignInAuthentication auth = await currentUser.authentication;

// Authenticate with firebase
final FirebaseUser user = await _auth.signInWithGoogle(
idToken: auth.idToken,
accessToken: auth.accessToken,
);

assert(user != null);
assert(!user.isAnonymous);

return user;
}


Future<Null> signOutWithGoogle() async {
debugPrint('in the SIGN OUT FUNCTION');
await _auth.signOut();
await _googleSignIn.signOut();

}

// ************** ENd Auth

void main() => runApp(new MyApp());


class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.yellow,
),
home: new SplashPage(),
routes: <String, WidgetBuilder>{
'/login': (BuildContext context) => new LoginPage(),
'/app': (BuildContext context) => new AppPage(),
},
);
}
}

class SplashPage extends StatefulWidget {
@override
State createState() => new _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
final FirebaseAuth _auth = FirebaseAuth.instance;



@override
void initState() {
super.initState();

_auth.onAuthStateChanged.firstWhere((user) => user != null).then((user) {
debugPrint('AUTH STATE HAS CHANGED');
debugPrint('user id: '+user.uid);
Navigator.of(context).pushReplacementNamed('/app');
});

new Future.delayed(new Duration(seconds: 1)).then((_) => signInWithGoogle());
}

@override
Widget build(BuildContext context) {
return new Text('splash 123');
}
}


class AppPage extends StatelessWidget {

void _logout(){
debugPrint('pressed logout button');
signOutWithGoogle();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('In Da App'),
actions: <Widget>[
new IconButton(icon: new Icon(Icons.list), onPressed: _logout),
],
),
body: new Text('Welcome'),
);
}
}

class LoginPage extends StatelessWidget {

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Text('You gotta login'),
);
}
}

为什么 onAuthStateChanged 不检测用户何时注销。此外,这是我单击注销按钮时控制台的屏幕截图。

enter image description here

根据控制台输出,我可以确认代码实际上正在根据我在那里的 debugPrint() 到达注销功能。我对控制台正在记录感到好奇:

"Notifying auth state listeners." 

然后立即记录:

"Notified 0 auth state listeners."

最佳答案

你应该使用 StreamBuilder以确保您的小部件得到通知。如果你看onAuthStateChanged它实际上返回一个 Stream<FirebaseUser>

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: new StreamBuilder(
stream: _auth.onAuthStateChanged,
builder: (context, snapshot) {
// Simple case
if (snapshot.hasData) {
return AppPage();
}

return SplashPage();
},
),
);
}
}

关于firebase - 如何在 Flutter 中将 onAuthStateChanged 用于 Firebase Auth?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com