Dart/Flutter Error: A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession'

392

cast await _authCategory.fetchAuthSession response to CognitoAuthSession.

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

SalahAdDin

Soy Ingeniero de Sistemas y Computación Egresado de la Universidad de Colombia, emprendedor, aprendo muy fácilmente y me gusta hacerlo autónomamente. Me encanta trabajar en Sistemas Operativos Linux, muy buenos para trabajar en programación y afines, y jugar en Windows. Actualmente me desempeño autónomamente en desarrollo de aplicaciones web. Me gusta ayudar y aprender. I'm a Computer and Systems Engineer graduated from the National University of Colombia, entrepreneur, I learn easily and I like to do it autonomously. I love Working on Linux Operative Systems, they are very good for working in programming and allied, and i like playing in Windows. Currently in involved in autonomous web applications development. I like to help and to learn.

Updated on December 29, 2022

Comments

  • SalahAdDin
    SalahAdDin over 1 year

    Our application requires to get the user role from Cognito, to solve this we followed the this comment but the linter(from Android Studio) and the running time show us the next bug:

    [+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(
    [        ] [        ]                                           ^
    

    The next is the repository's code where we are using 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());
        }
      }
    }
    

    It must be a CognitoAuthSession type variable in order to extract the session token and with it, to get the user's role(as you can see on the aforementioned comment).

    For more details please visit this 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
    

    May i be using this wrong?

    Thanks.