首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >触发Firebase [firebase_auth/网络请求-失败]网络错误(例如超时、中断连接或无法到达的主机)

触发Firebase [firebase_auth/网络请求-失败]网络错误(例如超时、中断连接或无法到达的主机)
EN

Stack Overflow用户
提问于 2022-07-21 10:14:27
回答 3查看 1.1K关注 0票数 1

我正在使用firebase实现Google登录,并将相应的用户信息存储在Cloud和共享首选项中。在我的手机上运行应用程序并点击登录/注册按钮时,会出现带有可用帐户的弹出。但是,当我选择所需的Google帐户时,弹出窗口就消失了,出现了如下错误:

firebase_auth/network-request-failed网络错误(如超时、中断连接或无法到达的主机)。

此外,没有帐户和用户详细信息既不存储在控制台中,也不存储在Firebase的用户部分。但是,细节存储在共享首选项中,并且在我重新运行HomePage代码时能够直接导航到application.My:

代码语言:javascript
运行
复制
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 {}

      
    }
  }
EN

Stack Overflow用户

发布于 2022-10-30 05:34:29

在这个错误中挣扎了几个小时,得到了人们指出的每一个解决方案。

其中一个终于解决了我的问题。我不知道哪一个能解决你的问题,所以什么都试一试。

  1. 关闭您的模拟器(关闭容纳本地主机的终端),
  2. 从表单标签中取出按钮,偏好div标记
  3. 将按钮从提交类型中删除,
  4. 为您的按钮添加这段代码(不冒泡):

< code >H19如果您确实在本地主机上进行测试,则选择127.0.0.1/而不是localhost /H 210G 211

代码语言:javascript
运行
复制
document.getElementById('signup-btn').addEventListener('click', function(event){
    event.preventDefault()

如果您使用的是"HTTPS“扩展,请转到"HTTPS”设置,然后单击“禁用该站点的HTTPS”按钮。它帮了我。

  1. 禁用CORS Chrome插件

  1. 确保传递的是实际值,而不是对按钮

的引用

  1. 更喜欢用铬而不是边缘进行测试。
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73064372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档