gpt4 book ai didi

flutter - Dart/flutter 错误 : A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession'

转载 作者:行者123 更新时间:2023-12-04 03:30:18 24 4
gpt4 key购买 nike

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

[+10900 ms] [+10852 ms] lib/auth/infrastructure/auth.repository.dart:61:43: Error: A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession'.
[ +3 ms] [ +2 ms] - 'AuthSession' is from 'package:amplify_auth_plugin_interface/src/Session/AuthSession.dart' ('/opt/flutter/.pub-cache/hosted/pub.dartlang.org/amplify_auth_plugin_interface-0.1.1/lib/src/Session/AuthSession.dart').
[ ] [ ] - 'CognitoAuthSession' is from 'package:amplify_auth_cognito/src/CognitoSession/CognitoAuthSession.dart' ('/opt/flutter/.pub-cache/hosted/pub.dartlang.org/amplify_auth_cognito-0.1.1/lib/src/CognitoSession/CognitoAuthSession.dart').
[ ] [ ] CognitoAuthSession fetchedSession = await _authCategory.fetchAuthSession(
[ ] [ ] ^

接下来是我们使用 Amplify Auth 的存储库代码:

import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_flutter/categories/amplify_categories.dart';

class AmplifyAuthRepository implements AuthRepository {
AmplifyAuthRepository({AuthCategory? authCategory})
: _authCategory = authCategory ?? Amplify.Auth;

final AuthCategory _authCategory;

@override
Future<Map<String, dynamic>> fetchSession() async {
try {
CognitoAuthSession fetchedSession = await _authCategory.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: true));
String token = fetchedSession.userPoolTokens.idToken;
Map<String, dynamic> payload = Jwt.parseJwt(token);
// Access the groups
List groups = payload['cognito:groups'];
Map<String, dynamic> result = {
'isSignedIn': fetchedSession.isSignedIn,
'roles': groups
};
return result;
} on AmplifyException catch (error) {
// TODO: Catch error for analytics, not required for frontend.
throw AmplifyException(error.toString());
}
}
}

它必须是 CognitoAuthSession 类型的变量才能提取 session token 并使用它来获取用户的角色(如您在上述评论中所见)。

更多详情请访问此issue .

Flutter (Channel stable, 2.0.4, on Linux, locale en_US.UTF-8)
• Flutter version 2.0.4 at /opt/flutter
• Framework revision b1395592de (7 days ago), 2021-04-01 14:25:01 -0700
• Engine revision 2dce47073a
• Dart version 2.12.2

我可能用错了吗?

谢谢。

最佳答案

将 await _authCategory.fetchAuthSession 响应转换到 CognitoAuthSession。

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

关于flutter - Dart/flutter 错误 : A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67017808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com