Page updated Nov 14, 2023

Manage user attributes

Amplify Flutter v0 is now in Maintenance Mode until July 19th, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v0.

Please use the latest version (v1) of Amplify Flutter to get started.

If you are currently using v0, follow these instructions to upgrade to v1.

Fetch the current user's attributes

Invoke the following api to get the list of attributes assigned to the user.

1Future<void> fetchCurrentUserAttributes() async {
2 try {
3 final result = await Amplify.Auth.fetchUserAttributes();
4 for (final element in result) {
5 print('key: ${element.userAttributeKey}; value: ${element.value}');
6 }
7 } on AuthException catch (e) {
8 print(e.message);
9 }
10}

Update user attribute

Invoke the update api for creating new or updating existing user attributes.

To update a single user attribute, call updateUserAttribute:

1Future<void> updateUserAttribute() async {
2 try {
3 final result = await Amplify.Auth.updateUserAttribute(
4 userAttributeKey: CognitoUserAttributeKey.email,
5 value: 'email@email.com',
6 );
7 if (result.nextStep.updateAttributeStep == 'CONFIRM_ATTRIBUTE_WITH_CODE') {
8 var destination = result.nextStep.codeDeliveryDetails?.destination;
9 print('Confirmation code sent to $destination');
10 } else {
11 print('Update completed');
12 }
13 } on AmplifyException catch (e) {
14 print(e.message);
15 }
16}

To update multiple user attributes at a time, call updateUserAttributes:

1Future<void> updateMultipleUserAttributes() async {
2 const attributes = [
3 AuthUserAttribute(
4 userAttributeKey: CognitoUserAttributeKey.email,
5 value: 'email@email.com',
6 ),
7 AuthUserAttribute(
8 userAttributeKey: CognitoUserAttributeKey.familyName,
9 value: 'MyFamilyName',
10 ),
11 ];
12
13 try {
14 final result = await Amplify.Auth.updateUserAttributes(attributes: attributes);
15 result.forEach((key, value) {
16 if (value.nextStep.updateAttributeStep == 'CONFIRM_ATTRIBUTE_WITH_CODE') {
17 final destination = value.nextStep.codeDeliveryDetails?.destination;
18 print('Confirmation code sent to $destination for $key');
19 } else {
20 print('Update completed for $key');
21 }
22 });
23 } on AmplifyException catch (e) {
24 print(e.message);
25 }
26}

Verify user attribute

Some attributes require confirmation for the attribute update to complete. If the attribute need to be confirmed, the result of the above api will be CONFIRM_ATTRIBUTE_WITH_CODE. A confirmation code will be sent to the delivery medium mentioned in the delivery details. When the user gets the confirmation code, you can present a UI to the user to enter the code and invoke the confirm attribute api with their input:

1Future<void> verifyAttributeUpdate() async {
2 try {
3 await Amplify.Auth.confirmUserAttribute(
4 userAttributeKey: CognitoUserAttributeKey.email,
5 confirmationCode: '390739',
6 );
7 print('Attribute verified');
8 } on AmplifyException catch (e) {
9 print(e.message);
10 }
11}

Resend verification code

If the code has expired or the user needs to resend the confirmation code, invoke the resend api as shown below:

1Future<void> resendVerificationCode() async {
2 try {
3 final result = await Amplify.Auth.resendUserAttributeConfirmationCode(
4 userAttributeKey: CognitoUserAttributeKey.email,
5 );
6 final destination = result.codeDeliveryDetails.destination;
7 print('Confirmation code set to $destination');
8 } on AmplifyException catch (e) {
9 print(e.message);
10 }
11}