Ary*_*ngh 8 firebase firebase-authentication flutter google-cloud-firestore
我正在使用 firebase auth 实现 Google 登录,并将相应的用户信息存储在 Cloud Firestore 和共享首选项中。在我的手机上运行该应用程序并点击登录/注册按钮时,会出现带有可用帐户的弹出窗口。但是当我选择所需的Google帐户时,弹出窗口消失并出现错误,如下:
[firebase_auth/network-request-failed] 发生网络错误(例如超时、连接中断或主机无法访问)。
此外,Cloud Firestore 控制台和 Firebase Auth 的“用户”部分中都不会存储任何帐户和用户详细信息。但详细信息存储在共享首选项中,并且当我重新运行应用程序时能够直接导航到主页。我的代码是:
class Login extends StatefulWidget {
static final String id = 'login_screen';
const Login({Key? key}) : super(key: key);
@override
State<Login> createState() => _LoginState();
}
class _LoginState extends State<Login> {
final GoogleSignIn googleSignIn = new GoogleSignIn();
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
late SharedPreferences preferences;
bool loading = false;
bool isLoggedIn = false;
User? user;
@override
void initState() {
super.initState();
isSignedIn();
}
void isSignedIn() async {
setState(() {
// loading = true;
});
preferences = await SharedPreferences.getInstance();
isLoggedIn = await googleSignIn.isSignedIn(); //Check if user is signed in
if (isLoggedIn) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
HomePage())); //Helps us to keep user logged in once he has logged in so that user doesn't come to log in screen again on pressing back.
setState(() {
loading = false;
});
}
}
Future signInWithGoogle() async {
preferences = await SharedPreferences.getInstance();
setState(() {
loading = true;
});
GoogleSignInAccount? googleUser = await googleSignIn.signIn();
if (googleUser != null) {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final UserCredential userCredential =
await firebaseAuth.signInWithCredential(credential);
user = userCredential.user;
if (user != null) {
final QuerySnapshot result = await FirebaseFirestore.instance
.collection("users")
.where("id", isEqualTo: user?.uid)
.get();
//Check whether the id of that field is equal to the id of the user we obtained above.
//If we have it, it means the user is already signed up to the application.
final List<DocumentSnapshot> docs = result.docs;
if (docs.length ==
0) //If the docs are empty means that user does not exist in our database, therfore sign hom up
{
//Add user to our collection
FirebaseFirestore.instance.collection("users").doc(user?.uid).set({
"id": user?.uid,
"username": user?.displayName,
"profilePicture": user?.photoURL,
"phNo": user?.phoneNumber,
"email": user?.email,
});
await preferences.setString('id', user!.uid);
await preferences.setString('userName', user!.displayName ?? ' ');
await preferences.setString('photoUrl', user!.photoURL ?? ' ');
await preferences.setString('email', user!.email ?? '');
} else {
await preferences.setString('id', docs[0]['id']);
await preferences.setString('userName', docs[0]['username']);
await preferences.setString('photoUrl', docs[0]['photoUrl']);
await preferences.setString('email', docs[0]['email']);
}
Navigator.popAndPushNamed(context, HomePage.id);
setState(() {
loading = false;
});
} else {}
}
}
Run Code Online (Sandbox Code Playgroud)
\xe2\x98\x91\xef\xb8\x8f 确保您的手机或模拟器可以访问互联网
\n\xe2\x98\x91\xef\xb8\x8f 另外请确保您已在文件中添加了ACCESS_INTERNET应用程序的权限AndroidManifest.xml
| 归档时间: |
|
| 查看次数: |
4712 次 |
| 最近记录: |