如何重定向到另一个页面后,谷歌在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();
}
}

我是这样做的

class Authentication {
static Future<User?> signInWithGoogle() async {
FirebaseAuth auth = FirebaseAuth.instance;
User? user;
final GoogleSignIn googleSignIn = GoogleSignIn();
final GoogleSignInAccount? googleSignInAccount =
await googleSignIn.signIn();
if (googleSignInAccount != null) {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
try {
final UserCredential userCredential =
await auth.signInWithCredential(credential);
user = userCredential.user;
} on FirebaseAuthException catch (e) {
if (e.code == 'account-exists-with-different-credential') {
Get.showSnackbar(const GetSnackBar(
message: "You already have an account with this email. Use other login method.",
duration: Duration(seconds: 3),
));
}
else if (e.code == 'invalid-credential') {
Get.showSnackbar(const GetSnackBar(
message: "Invalid Credential!",
duration: Duration(seconds: 3),
));
} else if (e.code == 'wrong-password') {
Get.showSnackbar(const GetSnackBar(
message: "Wrong password!",
duration: Duration(seconds: 3),
));
}
} catch (e) {
Get.showSnackbar(const GetSnackBar(
message: "Unknown Error. Try again later",
duration: Duration(seconds: 3),
));
}
}
return user;
}
}

那么在登录界面中,如果你成功登录,你可以添加这个来导航:

import 'your/google/sign/in/provider.dart';
import 'package:firebase_auth/firebase_auth.dart';
class Login {
User? user;
user = await Authentication.signInWithGoogle();
if (user != null) {
Navigator.pushReplacement<void, void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => const PageAfterSignIn(),));
} else {

}
}

我根据这篇文章写我的

方法FirebaseAuth.instance.signInWithCredential返回一个Future(Promise),它可以是用户成功登录也可以是用户未成功登录。

final UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential);
final user = userCredential.user;

如果你想移动到不同的屏幕,你可以使用Navigator。推动方法:

Navigator.push(
context,
MaterialPageRoute(builder: (context) => //YOUR SCREEN HERE),
);

文档

Firebase auth提供了一个authStateChanges(),它是一个Stream,所以每当一个新用户登录或注销时,它将被触发,在你的代码中,调用:

await FirebaseAuth.instance.signInWithCredential(credential);

如果运行成功,将触发它。

您可以通过使用StreamBuilder来收听authStateChanges()流,如下例所示:

StreamBuilder<User>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
User user = snapshot.data;
if (user == null) {
return LoginPage();
}
return HomePage();
} else {
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
},
);

这个小部件应该在你的应用程序的顶层运行(作为一个例子,在你的应用程序的MaterialApphome中使用它)

最初,当没有用户登录时,firebase的User将是null,因此它将重定向到LoginPage

在成功登录的操作上,例如代码中的FirebaseAuth.instance.signInWithCredential(credential);,User将有一些数据,它不会是null,因此StreamBuilder将得到通知,并显示HomePage()页面。

从登录页面点击google登录按钮后,您可以按照下面的代码进入下一屏幕

onTap: () async {
GoogleSignInProvider. googleLogin().then((isSuccess) {
if (isSuccess) {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => HomePage()));
}
});
},

最新更新