我仍然是新的Flutter,我想知道如何重定向到另一个页面后,登录到我的应用程序使用谷歌登录,有人能帮助这件事吗?这里的代码。我不知道在这种情况下重定向到另一个页面。谢谢
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? _user;
GoogleSignInAccount get user => _user!;
Future googleLogin() async{
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
notifyListeners();
}
}
4条答案
按热度按时间egdjgwm81#
这是我的
然后在登录屏幕中,如果您成功登录,则可以添加此内容以进行导航:
我基于this article编写我的
s4n0splo2#
方法FirebaseAuth.instance.signInWithCredential返回一个Future(Promise),它可以是用户是否成功登录。
如果您想移动到差异屏幕,您可以只使用Navigator.push方法:
Documentation
ny6fqffe3#
Firebase auth提供了一个
authStateChanges()
,这是一个Stream
,因此每次新用户登录或注销时,它都会被触发,在您的代码中调用:如果运行成功,将触发它。
您可以使用
StreamBuilder
来侦听authStateChanges()
流,如下例所示:此微件应在应用的顶层运行(例如,在应用的
home
或MaterialApp
中使用它)最初,当没有用户登录时,firebase的
User
将是null
,因此它将重定向到LoginPage
在成功登录的操作(如代码中的
FirebaseAuth.instance.signInWithCredential(credential);
)上,User
将包含一些数据,但它不是null
,因此StreamBuilder
将收到通知,并显示HomePage()
页面。olhwl3o24#
您可以在登录页面点击谷歌登录按钮后进入下一个屏幕,如下代码