【Flutter】表单 - Password

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【Flutter】表单 - Password相关的知识,希望对你有一定的参考价值。

参考技术A 参照
【Flutter】表单 - Input

首先创建一个密码框

设置右侧的小眼睛图标并绑定事件

Cognito 错误 I/flutter(4963):无效参数:需要 PASSWORD 参数

【中文标题】Cognito 错误 I/flutter(4963):无效参数:需要 PASSWORD 参数【英文标题】:Cognito Error I/flutter ( 4963): Invalid argument(s): PASSWORD parameter is required 【发布时间】:2020-01-03 05:52:52 【问题描述】:

我正在尝试按照 Flutter pub.dev 文档进行 Cognito 和 AWS 用户身份验证。我正在构建一个 Flutter 移动应用程序,并且能够成功地将用户注册到我的 AWS 用户池中。我正在尝试对登录功能进行编码,但是它不会注册我尝试提供的任何参数。即密码和用户名(电子邮件)

我尝试过重启电脑 Android Studios,尝试遵循所有 AWS 文档,并在 https://pub.dev/packages/amazon_cognito_identity_dart 上重新编写代码

AttributeArg ar1 = AttributeArg(name: 'USERNAME', value: userEmail); AttributeArg ar2 = AttributeArg(name: 'PASSWORD', value: userPassword);

final List<AttributeArg> authParams = [
  ar1,
  ar2,
];

AuthenticationDetails authDetails2 = AuthenticationDetails();

authDetails2.authParameters = authParams;

//final authDetails = new AuthenticationDetails(
//authParameters: authDetails2.getAuthParameters());

final authDetails =
    new AuthenticationDetails(authParameters: authDetails2.authParameters);

setCognitoUser();
CognitoUserSession session;

cognitoUser.setAuthenticationFlowType('USER_PASSWORD_AUTH');

try 
  //session = await cognitoUser.initiateAuth(authDetails);
  session = await cognitoUser.authenticateUser(authDetails);
  // username: userEmail, password: userPassword));
 catch (e) 
  print(e);

print(session.getAccessToken().getJwtToken());

I/flutter (4963):无效参数:需要 PASSWORD 参数 E/flutter(4963):[错误:flutter/lib/ui/ui_dart_state.cc(148)]未处理的异常:NoSuchMethodError:方法'getAccessToken'被调用为null。 E/颤动​​(4963):接收器:空 E/flutter(4963):尝试调用:getAccessToken() E/flutter (4963):#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5) E/flutter(4963):#1 Globals.logInUser(包:neurominer_app/globals.dart:76:19) E/颤动​​(4963): E/颤振(4963):#2 _LoginScreenState.build。 (包:neurominer_app/screens/login_screen.dart:155:34) E/flutter (4963):#3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14) E/颤振(4963):#4 _InkResponseState.build。 (包:flutter/src/material/ink_well.dart:711:32) E/flutter(4963):#5 GestureRecognizer.invokeCallback(包:flutter/src/gestures/recognizer.dart:182:24) E/flutter(4963):#6 TapGestureRecognizer._checkUp(包:flutter/src/gestures/tap.dart:365:11) E/flutter (4963): #7 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7) E/flutter (4963):#8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9) E/flutter (4963): #9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13) E/flutter (4963): #10 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11) E/flutter (4963): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19) E/flutter(4963):#12 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent(包:flutter/src/gestures/binding.dart:198:22) E/flutter (4963): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) E/flutter(4963):#14 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue(包:flutter/src/gestures/binding.dart:102:7) E/flutter (4963): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) E/flutter (4963):#16 _rootRunUnary (dart:async/zone.dart:1136:13) E/flutter (4963):#17 _CustomZone.runUnary (dart:async/zone.dart:1029:19) E/flutter (4963): #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7) E/flutter (4963):#19 _invoke1 (dart:ui/hooks.dart:250:10) E/flutter (4963): #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:159:5) E/颤动​​(4963):

【问题讨论】:

查看文档,您的 authDetails 变量需要是这样的: final authDetails = new AuthenticationDetails( username: 'email@inspire.my', password: 'Password001');所以我假设顶部数组中的“ar2”可能为空或无效。 我更改了一些代码并重新运行了所有内容。我相信问题出在一个单独的屏幕(我的登录屏幕)上,我没有发送非空值。因此它出现了用户名/密码为空。 【参考方案1】:

来自https://pub.dev/packages/amazon_cognito_identity_dart的正确代码

只需要确保由于我在多个屏幕(导入类和调用方法以进行用户身份验证)之间拆分我的代码,因此有效的用户名和密码(非空)值被用于身份验证。

final userPool = new CognitoUserPool(
    'ap-southeast-1_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
final cognitoUser = new CognitoUser(
    'email@inspire.my', userPool);
final authDetails = new AuthenticationDetails(
    username: 'email@inspire.my', password: 'Password001');
CognitoUserSession session;
try 
  session = await cognitoUser.authenticateUser(authDetails);
 catch(e)
  print(e);

【讨论】:

以上是关于【Flutter】表单 - Password的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - 将表单绑定到模型

Flutter Form表单控件超全总结

Flutter Form表单控件超全总结

Flutter 条件渲染 2 个表单 - 将表单值复制到另一个表单

【Flutter】表单 - Password

Flutter:文本表单字段的问题