因此,我目前正在设计一个应用程序,并首先制作一个登录页面,到目前为止,我已经开始登录/注册用户,但是,每次我尝试登录时,我都会收到一个网络错误,我不知道这是代码还是谷歌,因为我正在使用谷歌身份验证服务。
我已经按照教程在我的pubspec和build中设置了firebase,但它似乎仍然不起作用。
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/音频管理器(27253):playSoundEffect effectType: 0V/音频管理器(27253):querySoundEffectsEnabled...W/BiChannelGoogleApi(27253):com.google.firebase.auth.api.internal.zzak@744c74e : getGoogleApiForMethod()返回的Gms: FirebaseAuth W/DynamiteModule(27253):找不到com.google.firebase.auth的本地模块描述符类。I/FirebaseAuth(27253):FirebaseAuth:通过FirebaseOptions加载模块。I/FirebaseAuth(27253):flutter:正在准备创建到gms实现的服务连接I/flutter (27253):Error: PlatformException(ERROR_NETWORK_REQUEST_FAILED,发生网络错误(例如超时、连接中断或主机无法访问)。,FirebaseAuth)
发布于 2019-08-12 21:16:18
修复了这个问题,与网站API密钥相比,google-services文件中的API密钥不同,一旦我更改了它,它就工作得很好。
https://stackoverflow.com/questions/57452734
复制相似问题