【问题标题】:Firebase Network Issue?Firebase 网络问题?
【发布时间】:2019-12-18 12:49:11
【问题描述】:

所以我目前正在设计一个应用程序并首先制作一个登录页面,到目前为止我已经开始登录/注册用户,但是,每次我尝试登录时都会遇到网络错误,我不知道是不是代码或谷歌,因为我正在使用谷歌身份验证服务。

我已按照教程将 firebase 设置为我的 pubspec 并进行构建,但它似乎仍然无法正常工作。

    import 'package:flutter/material.dart';
    import 'package:firebase_auth/firebase_auth.dart';

    class LoginPage extends StatefulWidget {

      @override
      State<StatefulWidget> createState() => new _LoginPageState();
    }

    enum FormType {
      login,
      register
    }

    class _LoginPageState extends State<LoginPage> {

      final formKey = new GlobalKey<FormState>();

      String _email;
      String _password;
      FormType _formType = FormType.login;
      String _authHint = '';

      bool validateAndSave() {
        final form = formKey.currentState;
        if(form.validate()){
          form.save();
          return true;
        } else {
          return false;
        }
      }

      void validateAndSumbit() async {
      if (validateAndSave()) {
        try {
          if(_formType == FormType.login) {
      FirebaseUser user = (await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password)) as FirebaseUser;
      print('Signed in: ${user.uid}');
        } else {
          FirebaseUser user = (await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password)) as FirebaseUser;
          print('Registered user: ${user.uid}');
          }
        }
        catch (e) {
          print('Error: $e');
        }
      }
    }

      void moveToRegister() {
        formKey.currentState.reset();
        setState(() {
          _formType = FormType.register;
        });
      }

      void moveToLogin() {
        setState(() {
          _formType = FormType.login;
        });
      }

        @override
        Widget build(BuildContext context) {
          return new Scaffold(
            appBar: new AppBar(
              title:  new Text('Car Parking App'),
            ),
            body: new Container(
              padding: EdgeInsets.all(16.0),
              child: new Form(
                key: formKey,
                child: new Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: buildInputs() + buildSubmitButtons(),
                ),
              ),
            )
          );
        }

        List<Widget> buildInputs() {
          return [
        new TextFormField(
          decoration: new InputDecoration(labelText: 'Email'),
          validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
          onSaved: (value) => _email = value,
          ),
        new TextFormField(
          decoration: new InputDecoration(labelText: 'Password'),
          obscureText: true,
          validator: (value) => value.isEmpty ? 'Password can\'t be empty' : null,
          onSaved: (value) => _password = value,
          ),
          ];
        }

        List<Widget> buildSubmitButtons() {
          if (_formType == FormType.login) {
          return [
                  new RaisedButton(
            child: new Text('Login', style: new TextStyle(fontSize: 20.0)),
            onPressed: validateAndSumbit,
          ),
          new FlatButton(
            child: new Text('Create an Account', style: new TextStyle(fontSize: 20.0)),
            onPressed: moveToRegister,
          )
          ];
        } else {
                return [
                  new RaisedButton(
            child: new Text('Create an Account', style: new TextStyle(fontSize: 20.0)),
            onPressed: validateAndSumbit,
          ),
          new FlatButton(
            child: new Text('Already have an account? Login', style: new TextStyle(fontSize: 20.0)),
            onPressed: moveToLogin,
          )
          ];
        }
        }
    }

V/AudioManager(27253): playSoundEffect effectType: 0 V/AudioManager(27253):querySoundEffectsEnabled... W/BiChannelGoogleApi(27253): [FirebaseAuth: ] getGoogleApiForMethod() 返回 Gms: com.google.firebase.auth.api.internal.zzak@744c74e W/DynamiteModule(27253):未找到 com.google.firebase.auth 的本地模块描述符类。 I/FirebaseAuth(27253): [FirebaseAuth:] 通过 FirebaseOptions 加载模块。 I/FirebaseAuth(27253): [FirebaseAuth:] 准备创建与 gms 实施的服务连接 I/flutter (27253): Error: PlatformException(ERROR_NETWORK_REQUEST_FAILED, A network error (such as timeout, interrupted connection or unreachable host) has occurred., null)

【问题讨论】:

  • 可能性太多。请试试这个stackoverflow.com/questions/40052162/…。你是否包括 在你的 AndroidManifest.xml 中
  • 请告诉我您的测试结果。谢谢。
  • 在将其输入 manifest.xml 后,我现在收到关于我的 API 密钥无效的错误

标签: java android flutter dart visual-studio-code


【解决方案1】:

修复了这个问题,与网站 API 密钥相比,google-services 文件中的 API 密钥不同,一旦我更改它就可以正常工作。

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 2021-10-13
    • 2016-03-15
    • 2011-06-01
    • 2011-07-04
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多