Home > front end >  Firebase Infinite Loading after Login/Register
Firebase Infinite Loading after Login/Register

Time:10-05

so i am working on an app which uses firebase.
But I am having an issue although this has been solved that doesnt seem to help me. My loading screen goes to infinite loading after login/register.
Login Code

import 'package:flutter/material.dart';
import 'package:studo/services/auth.dart';
import 'package:studo/shared/constants.dart';
import 'package:studo/shared/loading.dart';

class SignIn extends StatefulWidget {
  final Function toggleView;
  SignIn({this.toggleView});

  @override
  _SignInState createState() => _SignInState();
}

class _SignInState extends State<SignIn> {
  final AuthService _auth = AuthService();
  final _formKey = GlobalKey<FormState>();
  String error = '';
  bool loading = false;

  // text field state
  String email = '';
  String password = '';

  @override
  Widget build(BuildContext context) {
    return loading
        ? Loading()
        : Scaffold(
            body: SingleChildScrollView(
              child: Center(
                child: Container(
                  padding:
                      EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
                  child: Form(
                    key: _formKey,
                    child: Column(
                      children: <Widget>[
                        SizedBox(height: 30.0),
                        CircleAvatar(
                          backgroundImage: AssetImage('assets/login.png'),
                          backgroundColor: Colors.transparent,
                          radius: 150,
                        ),
                        TextFormField(
                          decoration: textInputDecoration.copyWith(
                              hintText: 'Email',
                              hintStyle: TextStyle(color: Colors.purple)),
                          validator: (val) =>
                              val.isEmpty ? 'Enter an email' : null,
                          onChanged: (val) {
                            setState(() => email = val);
                          },
                        ),
                        SizedBox(height: 20.0),
                        TextFormField(
                          obscureText: true,
                          decoration: textInputDecoration.copyWith(
                              hintText: 'Password',
                              hintStyle: TextStyle(color: Colors.purple)),
                          validator: (val) => val.length < 6
                              ? 'Enter a password 6  chars long'
                              : null,
                          onChanged: (val) {
                            setState(() => password = val);
                          },
                        ),
                        SizedBox(height: 20.0),
                        RaisedButton(
                            color: Colors.purple,
                            child: Text(
                              'Login',
                              style: TextStyle(color: Colors.white),
                            ),
                            onPressed: () async {
                              if (_formKey.currentState.validate()) {
                                setState(() => loading = true);
                                dynamic result =
                                    await _auth.signInWithEmailAndPassword(
                                        email, password);
                                if (result == null) {
                                  setState(() {
                                    loading = false;
                                    error =
                                        'Could not sign in with those credentials';
                                  });
                                }
                              }
                            }),
                        SizedBox(height: 12.0),
                        Text(
                          error,
                          style: TextStyle(color: Colors.red, fontSize: 14.0),
                        ),
                        GestureDetector(
                          onTap: () {
                            Navigator.pushReplacementNamed(
                                context, '/register');
                          },
                          child: Text(
                            'Dont have an acoount Sign Up',
                            style: TextStyle(color: Colors.purple),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
  }
}

Loading.dart code

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

class Loading extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Center(
        child: Column(
          children: [
            SizedBox(height: 50,),
             SpinKitChasingDots(
                color: Colors.purple,
                size: 50.0,
              ),
            SizedBox(height: 70,),
            Text('Please Exit the App and then Open Again Due to an issue. The Home Page will Load again',
              style: TextStyle(fontSize: 10),
            )
          ],
        ),
      ),
    );
  }
}

A help would be greatly appreciated. i think i am wrong with the on pressed part but i cant possibly figure that out. PLease help me

CodePudding user response:

In your onPressed you are only handling the result == null. You should also handle when you have a result, such that you can go to a next page when returned result is logged in, or set loading = false.

This can be done with an else statement, because if result != null you want to handle the result.

The result should be of the class UserCredential, which contains the User class.

It could be that the email or password is incorrect, you want to handle these cases as well, good practice is to do this in a try-catch block. Example from firebase

try {
  UserCredential userCredential = await 
FirebaseAuth.instance.signInWithEmailAndPassword(
    email: "[email protected]",
    password: "SuperSecretPassword!"
  );
} on FirebaseAuthException catch (e) {
  if (e.code == 'user-not-found') {
    print('No user found for that email.');
  } else if (e.code == 'wrong-password') {
    print('Wrong password provided for that user.');
  }
}

CodePudding user response:

You are always calling setState(() => loading = true) please add a check before it as demonstrated below I think it should do

  if(loading == false){
    setState(() => loading = true);
  }
  • Related