Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 省道/颤振错误:类型为';AuthSession';can';不能分配给类型为';CognitoAuthSession';_Flutter_Dart_Amazon Cognito_Aws Amplify - Fatal编程技术网

Flutter 省道/颤振错误:类型为';AuthSession';can';不能分配给类型为';CognitoAuthSession';

Flutter 省道/颤振错误:类型为';AuthSession';can';不能分配给类型为';CognitoAuthSession';,flutter,dart,amazon-cognito,aws-amplify,Flutter,Dart,Amazon Cognito,Aws Amplify,我们的应用程序需要从Cognito获取用户角色,为了解决这个问题,我们遵循了前面的步骤,但是linter(来自Android Studio)和运行时间向我们显示了下一个bug: [+10900 ms][+10852 ms]lib/auth/infrastructure/auth.repository.dart:61:43:错误:“AuthSession”类型的值无法分配给“CognitoAuthSession”类型的变量。 [+3毫秒][+2毫秒]-“AuthSession”来自“package

我们的应用程序需要从Cognito获取用户角色,为了解决这个问题,我们遵循了前面的步骤,但是linter(来自Android Studio)和运行时间向我们显示了下一个bug:

[+10900 ms][+10852 ms]lib/auth/infrastructure/auth.repository.dart:61:43:错误:“AuthSession”类型的值无法分配给“CognitoAuthSession”类型的变量。
[+3毫秒][+2毫秒]-“AuthSession”来自“package:amplify_-auth_-plugin_-interface/src/Session/AuthSession.dart”(“/opt/flatter/.pub-cache/hosted/pub.dartlang.org/amplify_-auth_-plugin_-interface-0.1.1/lib/src/Session/AuthSession/AuthSession.dart”)。
[]]-“CognitoAuthSession”来自“package:amplify_-auth_-cognito/src/CognitoSession/CognitoAuthSession.dart”(“/opt/flatter/.pub cache/hosted/pub.dartlang.org/amplify_-auth_-cognito-0.1.1/lib/src/CognitoAuthSession/CognitoAuthSession.dart”)。
[]CognitoAuthSession fetchedSession=Wait\u authCategory.fetchAuthSession(
[        ] [        ]                                           ^
下一个是存储库的代码,我们在其中使用了放大Auth

import'package:amplify_auth_cognito/amplify_auth_cognito.dart';
进口“包装:放大颤振/放大飞镖”;
导入“包:放大颤振/类别/放大类别.dart”;
类AuthRepository实现AuthRepository{
AuthRepository({AuthCategory?AuthCategory})
:_authCategory=authCategory??放大.Auth;
最终AuthCategory _AuthCategory;
@凌驾
Future fetchSession()异步{
试一试{
CognitoAuthSession fetchedSession=等待_authCategory.fetchAuthSession(
选项:CognitoSessionOptions(getAWSCredentials:true));
String token=fetchedSession.userPoolTokens.idToken;
Map payload=Jwt.parseJwt(令牌);
//访问组
列表组=有效负载['cognito:groups'];
映射结果={
'isSignedIn':fetchedSession.isSignedIn,
“角色”:组
};
返回结果;
}关于异常捕获(错误){
//TODO:捕捉分析错误,前端不需要。
抛出异常(error.toString());
}
}
}
它必须是
CognitoAuthSession
类型变量,才能提取会话令牌并使用它获取用户角色(如前面的注释所示)

有关更多详细信息,请访问此

我可能用错了吗


谢谢。

cast wait\u authcography.fetchAuthSession对CognitoAuthSession的响应

CognitoAuthSession fetchedSession =
        await _authCategory.fetchAuthSession(options: CognitoSessionOptions(getAWSCredentials: true)) as CognitoAuthSession;
    print('fetchedSession ${fetchedSession.userPoolTokens?.idToken}');

您使用的是哪个版本的
amplify\u Flatter
amplify\u auth\u cognito
?@raviseta
amplify\u analytics\u pinpoint:^0.1.1 amplify\u auth\u cognito:^0.1.1 amplify\u storage\u s3:^0.1.1.1
如果您尝试过
Flatter clean
,我又犯了错误。实际上,它在我的机器上没有产生错误,所以要捕捉到确切的错误有点困难。@SalahAdDinthank for correction@Amit_PYes,在Dart中,强制转换到子类就是这样做的。如果实例不属于强制转换到类,则强制转换将失败。您还可以先将一个(会话是CognitoAuthSession)。在由
if
语句控制的块内,变量将被视为
CognitoAuthSession
类型。
CognitoAuthSession fetchedSession =
        await _authCategory.fetchAuthSession(options: CognitoSessionOptions(getAWSCredentials: true)) as CognitoAuthSession;
    print('fetchedSession ${fetchedSession.userPoolTokens?.idToken}');