Flutter AWS Auth: How to get custom user attribute?

Viewed 264

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 Answers

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.

Related