Stuck on Spinkit loading screen when submit is tapped

0

I'm new to Flutter and hope I can get help from you lovely folk as I think after a few days of trying to crack this my hair are turning grey. Sorry if my terminology below is a little off!

I've set up an app with a Cloud Firestore that'll let users log in, they then have access to a list of options they can choose from - once they tap submit their info will go to a Google Sheets spreadsheet.

The records of registers and sign ins appear in Cloud Firestore and the submitted info goes to the spreadsheet, but for some reason when tapping either 'Log in' on the sign in page or 'submit' on the register page it stays on the loading screen (I use a Spinkit) - all other buttons (such as on the appbar) work.

I created a project where I kept the Firebase code, but not the Google Sheets code and it navigates fine.

I think the problem lies in the following code:

register.dart

dynamic result = await _auth.registerWithEmailAndPassword(email.trim(), password);
    if(result == null) {
      setState(() {
        error = 'Please provide a valid email';
        loading = false;
      });
    }

...as if I remove it and replace with...

Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => MyHomePage()),
);

...the screens change, but then this stops the new sign in from appearing on Cloud Firestore.

register.dart (full)

import 'package:epddiary/screens/authenticate/sign_in.dart';
import 'package:epddiary/services/auth.dart';
import 'package:epddiary/shared/loading.dart';
import 'package:flutter/material.dart';

class Register extends StatefulWidget {

  final Function toggleView;
  Register({ this.toggleView });

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

class _RegisterState extends State<Register> {

  final AuthService _auth = AuthService();
  final _formKey = GlobalKey<FormState>();
  bool loading = false;

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

  @override
  Widget build(BuildContext context) {
    return loading ? Loading() : Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('EPD Site Diary'),
        backgroundColor: Color(0xFF178943),
        elevation: 0.0,
        actions: <Widget>[

          FlatButton(
            child: Text('SIGN IN',
              style: TextStyle(
                color: Colors.white,
              ),
            ),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => SignIn()),
                );
              }
          ),
        ],

      ),
      body: Container(
        padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 50.0),
        child: Form(
          key: _formKey,
          child: ListView(
            children: <Widget>[

              SizedBox(height: 20.0),

              TextFormField(
                validator: (val) => val.isEmpty ? 'Enter an email' : null,
                onChanged: (val) {
                  setState(() => email = val);
                },
                decoration: InputDecoration(
                  enabledBorder: const OutlineInputBorder(
                    // width: 0.0 produces a thin "hairline" border
                    borderSide: const BorderSide(color: Color(0xFFe8e8e8), width: 1.5),
                    borderRadius: const BorderRadius.all(
                      const Radius.circular(100.0),
                    ),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Color(0xFF178943), width: 1.5),
                    borderRadius: const BorderRadius.all(const Radius.circular(100.0),),
                  ),
                  contentPadding: EdgeInsets.only(top: 4.0),
                  focusColor: Color(0xFF178943),
                  prefixIcon: Icon(
                    Icons.email,
                    color: Color(0xFF178943),
                  ),
                  hintText: 'Enter your Email',
                  fillColor: Color(0xFFFFFFFF),
                  filled: true,
                ),
              ),

              SizedBox(height: 20.0),

              TextFormField(
                obscureText: true,
                validator: (val) => val.length < 6 ? 'Password must be 6 or more characters long' : null,
                onChanged: (val) {
                  setState(() => password = val);
                },
                decoration: InputDecoration(
                  enabledBorder: const OutlineInputBorder(
                    // width: 0.0 produces a thin "hairline" border
                    borderSide: const BorderSide(color: Color(0xFFe8e8e8), width: 1.5),
                    borderRadius: const BorderRadius.all(
                      const Radius.circular(100.0),
                    ),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Color(0xFF178943), width: 1.5),
                    borderRadius: const BorderRadius.all(const Radius.circular(100.0),),
                  ),
                  contentPadding: EdgeInsets.only(top: 14.0),
                  focusColor: Color(0xFF178943),
                  prefixIcon: Icon(
                    Icons.lock,
                    color: Color(0xFF178943),
                  ),
                  hintText: 'Enter a Password',
                  fillColor: Color(0xFFFFFFFF),
                  filled: true,
                ),
              ),

              SizedBox(height: 20.0),

              FlatButton(
                  color: Color(0xFF178943),
                  child: Text(
                    'Submit',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 24.0,
                      fontFamily: 'Lato',
                      letterSpacing: 1,
                    ),
                  ),
                  padding: EdgeInsets.all(10.0,),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(50.0),
                  ),
                  onPressed: () async {
                    if(_formKey.currentState.validate()){
                      setState(() => loading = true);
                      dynamic result = await _auth.registerWithEmailAndPassword(email.trim(), password);
                      if(result == null) {
                        setState(() {
                          error = 'Please provide a valid email';
                          loading = false;
                        });
                      }
                    }
                  }

              ),
              SizedBox(height: 12.0),
              Text(
                error,
                style: TextStyle(color: Colors.red, fontSize: 14.0),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Here's where I call _auth

auth.dart

import 'package:epddiary/models/user.dart';
import 'package:epddiary/services/database.dart';
import 'package:firebase_auth/firebase_auth.dart';

class AuthService {

  final FirebaseAuth _auth = FirebaseAuth.instance;

  // create user obj based on firebase user
  User _userFromFirebaseUser(FirebaseUser user) {
    return user != null ? User(uid: user.uid) : null;
  }

  // auth change user stream
  Stream<User> get user {
    return _auth.onAuthStateChanged
      //.map((FirebaseUser user) => _userFromFirebaseUser(user));
      .map(_userFromFirebaseUser);
  }

  // sign in anon
  Future signInAnon() async {
    try {
      AuthResult result = await _auth.signInAnonymously();
      FirebaseUser user = result.user;
      return _userFromFirebaseUser(user);
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  // sign in with email and password
  Future signInWithEmailAndPassword(String email, String password) async {
    try {
      AuthResult result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      return user;
    } catch (error) {
      print(error.toString());
      return null;
    } 
  }

  // register with email and password
  Future registerWithEmailAndPassword(String email, String password) async {
    try {
      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      // create a new document for the user with the uid
      await DatabaseService(uid: user.uid).updateUserData('','','','','','','','');
      return _userFromFirebaseUser(user);
    } catch (error) {
      print(error.toString());
      return null;
    }
  }

  // sign out
  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (error) {
      print(error.toString());
      return null;
    }
  }

}
Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
Parameter format not correct -
√ Built build\app\outputs\apk\debug\app-debug.apk.
Installing build\app\outputs\apk\app.apk...
E/eglCodecCommon( 5912): glUtilsParamSize: unknow param 0x000082da
D/EGL_emulation( 5912): eglMakeCurrent: 0xe0f056c0: ver 3 1 (tinfo 0xe0f03900)
Debug service listening on ws://127.0.0.1:11606/WN06hS2fS0M=/ws
Syncing files to device AOSP on IA Emulator...
D/eglCodecCommon( 5912): setVertexArrayObject: set vao to 0 (0) 1 2
D/EGL_emulation( 5912): eglMakeCurrent: 0xe0f05960: ver 3 1 (tinfo 0xe0f037f0)
D/eglCodecCommon( 5912): setVertexArrayObject: set vao to 0 (0) 1 0
I/flutter ( 5912): null
I/flutter ( 5912): Instance of 'User'
W/IInputConnectionWrapper( 5912): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper( 5912): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 5912): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper( 5912): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 5912): endBatchEdit on inactive InputConnection
I/BiChannelGoogleApi( 5912): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@3a2c8e
D/FirebaseAuth( 5912): Notifying id token listeners about user ( tTtIWQsRIHPVEragEg9W872VE9l1 ).
D/FirebaseAuth( 5912): Notifying auth state listeners about user ( tTtIWQsRIHPVEragEg9W872VE9l1 ).
I/flutter ( 5912): Instance of 'User'
W/DynamiteModule( 5912): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 5912): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 5912): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/m.epdsite.diar( 5912): The ClassLoaderContext is a special shared library.
I/chatty  ( 5912): uid=10085(com.epdsite.diary) AsyncTask #2 identical 2 lines
I/m.epdsite.diar( 5912): The ClassLoaderContext is a special shared library.
W/m.epdsite.diar( 5912): Accessing hidden field Ldalvik/system/BaseDexClassLoader;->pathList:Ldalvik/system/DexPathList; (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden field Ldalvik/system/DexPathList;->nativeLibraryDirectories:Ljava/util/List; (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden field Ldalvik/system/DexPathList;->systemNativeLibraryDirectories:Ljava/util/List; (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden field Ldalvik/system/DexPathList;->nativeLibraryPathElements:[Ldalvik/system/DexPathList$NativeLibraryElement; (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden method Ldalvik/system/DexPathList;->makePathElements(Ljava/util/List;)[Ldalvik/system/DexPathList$NativeLibraryElement; (light greylist, reflection)
V/NativeCrypto( 5912): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 284 native methods...
W/m.epdsite.diar( 5912): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
D/NetworkSecurityConfig( 5912): No Network Security Config specified, using platform default
I/ProviderInstaller( 5912): Installed default security provider GmsCore_OpenSSL
W/m.epdsite.diar( 5912): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, linking)
W/m.epdsite.diar( 5912): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, linking)
W/m.epdsite.diar( 5912): Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, JNI)
W/m.epdsite.diar( 5912): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
W/m.epdsite.diar( 5912): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
W/m.epdsite.diar( 5912): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
C:\src\flutter\bin\flutter.bat doctor --verbose
[√] Flutter (Channel stable, v1.17.2, on Microsoft Windows [Version 10.0.18362.900], locale en-GB)
    • Flutter version 1.17.2 at C:\src\flutter
    • Framework revision 5f21edf8b6 (2 weeks ago), 2020-05-28 12:44:12 -0700
    • Engine revision b851c71829
    • Dart version 2.8.3

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at C:\Users\NiallASD\AppData\Local\Android\sdk
    • Platform android-29, build-tools 29.0.3
    • Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
    • All Android licenses accepted.

[√] Android Studio (version 3.6)
    • Android Studio at C:\Program Files\Android\Android Studio1
    • Flutter plugin version 45.1.1
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)

[√] VS Code (version 1.46.0)
    • VS Code at C:\Users\NiallASD\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.11.0

[√] Connected device (1 available)
    • AOSP on IA Emulator • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

• No issues found!
Process finished with exit code 0

I used this GitHub for the Google Sheets spreadsheet, which originally had the code in main.dart (I moved it to home.dart).

Hope this covers my issue clear enough (please let me know if more information is needed).

Many thanks for reading this far!

firebase
flutter
google-sheets
spreadsheet
asked on Stack Overflow Jun 13, 2020 by Niall Devlin • edited Jun 13, 2020 by Frank van Puffelen

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0