'Flutter AWS Auth: How to get custom user attribute?

How do I get in Flutter, the cognito custom user attribute for user?

await Amplify.Auth.fetchUserAttributes();

returns only user attributes but not the custom defined ones.

(I have added the attribute to the schema and I am sure it's there, in the AWS UI it's there.)



Solution 1:[1]

The issue was that these custom attributes, after they are created, they are not by default readable or writable.

For further explanations, check https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-attribute-permissions-and-scopes Go to cognito - app clients - details - and very bottom to change permissions

Go to cognito - app clients - details - and very bottom to change permissions

Solution 2:[2]

The fetchUserAttributes function returns a list of AuthUserAttributes including the custom ones you've defined. When you have that list you can iterate through it, and get the attributes you want.

const res = await Amplify.Auth.fetchUserAttributes();

for (var attr in res) {
  if (attr.userAttributeKey == CognitoUserAttributeKey.custom('customAttr') {
    customAttr = attr.value;
  }
}

If the custom attribute isn't there, make sure the user have that attribute.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 JoakimMellonn