Search code examples
amazon-web-servicesflutterdartflutter-layoutdart-pub

How to fix error Flutter Login via AWS Cognito


I was writing code for user login on AWS using flutter and here is my code

I was trying to create a login screen with AWS Cognito on flutter

Submit Method
 submit(BuildContext context) async {
    _formKey.currentState.save();
    String message;
    try {
      _user = await _userService.login(_user.email, _user.password);
      message = 'User sucessfully logged in!';
      if (!_user.confirmed) {
        message = 'Please confirm user account';
      }
    } on CognitoClientException catch (e) {
      if (e.code == 'InvalidParameterException' ||
          e.code == 'NotAuthorizedException' ||
          e.code == 'UserNotFoundException' ||
          e.code == 'ResourceNotFoundException') {
        message = e.message;
      } else {
        message = 'An unknown client error occured';
      }
    } catch (e) {
      message = 'An unknown error occurred';
    }
    final snackBar = new SnackBar(
      content: new Text(message),
      action: new SnackBarAction(
        label: 'OK',
        onPressed: () async {
          if (_user.hasAccess) {
            Navigator.pop(context);
            if (!_user.confirmed) {
              Navigator.push(
                context,
                new MaterialPageRoute(
                    builder: (context) =>
                    new ConfirmationScreen(email: _user.email)),
              );
            }
          }
        },
      ),
      duration: new Duration(seconds: 30),
    );

    Scaffold.of(context).showSnackBar(snackBar);
  }

on Button Click, the submit method is being called


                   Container(
                      height: 40.0,
                      child: Material(
                        borderRadius: BorderRadius.circular(20.0),
                        shadowColor: Colors.blueAccent,
                        color: Colors.blue,
                        elevation: 7.0,
                        child: GestureDetector(
                          onTap: () {
                              submit(context);
                          },
                          child: Center(
                            child: Text(
                              'LOGIN',
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontFamily: 'Montserrat'),
                            ),
                          ),
                        ),
                      ),
                    ),
This is the error that I'm seeing on the terminal when I click on the login button

E/flutter (28368): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'save' was called on null.
E/flutter (28368): Receiver: null
E/flutter (28368): Tried calling: save()
E/flutter (28368): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter (28368): #1      _LoginScreenState.submit (package:flutter_ui_login/main.dart:69:27)
E/flutter (28368): <asynchronous suspension>
E/flutter (28368): #2      _LoginScreenState.build.<anonymous closure> (package:flutter_ui_login/main.dart:196:31)
E/flutter (28368): #3      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
E/flutter (28368): #4      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240:9)
E/flutter (28368): #5      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:177:9)
E/flutter (28368): #6      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436:9)
E/flutter (28368): #7      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (28368): #8      PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (28368): #9      _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:221:19)
E/flutter (28368): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:199:22)
E/flutter (28368): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (28368): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (28368): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (28368): #14     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (28368): #15     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (28368): #16     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (28368): #17     _invoke1 (dart:ui/hooks.dart:233:10)
E/flutter (28368): #18     _dispatchPointerDataPacket (dart:ui/hooks.dart:154:5)
E/flutter (28368): 

I am not seeing any change on the app when I click on login and on the terminal I see the above messege. I was developing a small app for Cognito user login into AWS. If anyone of you has a different code for AWS Login, then please help me with that.


Solution

  • Not a Cognito issue, as Sébastien Stormacq mentioned, since the Cognito code which I imagine lives in your _userService, didn't get called.

    Seems like the submit() method cannot read the _formkey property. Does your submit() method live within a State class that declares a _formkey?

    Here's a sample structure adapted from the Flutter official guide

    // Create a Form widget.
    class MyCustomForm extends StatefulWidget {
      @override
      MyCustomFormState createState() {
        return MyCustomFormState();
      }
    }
    
    // Create a corresponding State class.
    // This class holds data related to the form.
    class MyCustomFormState extends State<MyCustomForm> {
      // Create a global key that uniquely identifies the Form widget
      // and allows validation of the form.
      //
      // Note: This is a GlobalKey<FormState>,
      // not a GlobalKey<MyCustomFormState>.
      final _formKey = GlobalKey<FormState>();
    
      @override
      Widget build(BuildContext context) {
        // Build a Form widget using the _formKey created above.
        return Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: Container(
    
            // declare your Container layout and your submit button logic here
            // _formkey.currentState will be defined  
    
            )
      }
    }