我正在使用firebase实现Google登录,并将相应的用户信息存储在Cloud和共享首选项中。在我的手机上运行应用程序并点击登录/注册按钮时,会出现带有可用帐户的弹出。但是,当我选择所需的Google帐户时,弹出窗口就消失了,出现了如下错误:
firebase_auth/network-request-failed网络错误(如超时、中断连接或无法到达的主机)。
此外,没有帐户和用户详细信息既不存储在控制台中,也不存储在Firebase的用户部分。但是,细节存储在共享首选项中,并且在我重新运行HomePage代码时能够直接导航到application.My:
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 {}
}
}发布于 2022-07-21 11:26:43
这可能是由于你的互联网连接
☑️确保您的手机或仿真器能够访问internet
ACCESS_INTERNET还确保您在AndroidManifest.xml文件中向应用程序添加了☑️权限。
发布于 2022-07-21 11:32:53
您是否在firebase项目设置中添加了SHA证书指纹?不添加SHA可能会导致此问题。
发布于 2022-10-30 05:34:29
在这个错误中挣扎了几个小时,得到了人们指出的每一个解决方案。
其中一个终于解决了我的问题。我不知道哪一个能解决你的问题,所以什么都试一试。
< code >H19如果您确实在本地主机上进行测试,则选择127.0.0.1/而不是localhost /H 210G 211
document.getElementById('signup-btn').addEventListener('click', function(event){
event.preventDefault()
如果您使用的是"HTTPS“扩展,请转到"HTTPS”设置,然后单击“禁用该站点的HTTPS”按钮。它帮了我。
的引用
https://stackoverflow.com/questions/73064372
复制相似问题