Firebase网络问题?

时间:2019-08-11 18:56:28

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

因此,我目前正在设计一个应用程序,首先创建一个登录页面,到目前为止,我已经达到登录/注册用户的水平,但是,每次尝试登录时,我都会遇到网络错误,并且无法确定是否是代码或我正在使用Google身份验证服务的Google。

我遵循了有关将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):错误:PlatformException(ERROR_NETWORK_REQUEST_FAILED,发生了网络错误(例如超时,连接中断或主机不可达)。,为空)

1 个答案:

答案 0 :(得分:0)

解决了这个问题,一旦我更改为可以正常使用,与网站API密钥相比,google-services文件中的API密钥就不同了。