So I'm currently designing an App and firstly making a login page, I so far got up to login/registering users, however, every time I try to login I get a network error and I can't tell if it's the code or Google as I am using google authentication services.
I've followed tutorials on setting up the firebase to my pubspec and build but it still doesn't seem to work.
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() returned Gms: com.google.firebase.auth.api.internal.zzak@744c74e W/DynamiteModule(27253): Local module descriptor class for com.google.firebase.auth not found. I/FirebaseAuth(27253): [FirebaseAuth:] Loading module via FirebaseOptions. I/FirebaseAuth(27253): [FirebaseAuth:] Preparing to create service connection to gms implementation I/flutter (27253): Error: PlatformException(ERROR_NETWORK_REQUEST_FAILED, A network error (such as timeout, interrupted connection or unreachable host) has occurred., null)