我正在使用Flutter在AWS上编写用于用户登录的代码,这是我的代码
我试图用AWS Cognito创建一个登录屏幕
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);
}
单击按钮,将调用提交方法
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):
当我单击登录时,在终端上看不到上面的消息,应用程序没有任何变化。 我正在为Cognito用户登录到AWS开发一个小型应用程序。如果您的任何人都有不同的AWS登录代码,请帮助我。
答案 0 :(得分:0)
我不认识Flutter,我认识Cognito。您似乎收到的错误消息与Cognito无关。
Atom 1.25
重要的部分是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
,所以当您调用此The method 'save' was called on null
方法时,我查看了您的代码,这似乎是您进行的第一个调用,甚至在调用Cognito之前:{{1 }}
因此,您需要了解为什么save()
为空。
恕我直言,这不是Cognito问题。
答案 1 :(得分:0)
不是Sébastien Stormacq所述的Cognito问题,因为我想象中的Cognito代码没有在您的_userService中被调用。
似乎submit()方法无法读取_formkey属性。 您的commit()方法是否存在于声明_formkey的State类中?
改编而成的示例结构// 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
)
}
}