Flutter AWS Auth: How to get custom user attribute?

1.4k views Asked by At

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.)

2

There are 2 answers

0
radrt On BEST ANSWER

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

4
JoakimMellonn On

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.