Skip to content

[amplify-libraries][Flutter] Document copyWithModelFieldValues for null fields - #8619

Open
AzazelSensei wants to merge 3 commits into
aws-amplify:mainfrom
AzazelSensei:docs/5019-copyWithModelFieldValues-null
Open

[amplify-libraries][Flutter] Document copyWithModelFieldValues for null fields#8619
AzazelSensei wants to merge 3 commits into
aws-amplify:mainfrom
AzazelSensei:docs/5019-copyWithModelFieldValues-null

Conversation

@AzazelSensei

Copy link
Copy Markdown

Description of changes:

Flutter copyWith on generated models treats a null argument as "keep the current value", so it cannot clear a nullable field. The generated copyWithModelFieldValues API does that via ModelFieldValue.value(null).

Added that to the Flutter DataStore update snippet and the GraphQL mutate example.

Related GitHub issue #, if available:

aws-amplify/amplify-flutter#5019

Instructions

Which product(s) are affected by this PR (if applicable)?

  • amplify-cli
  • amplify-ui
  • amplify-studio
  • amplify-hosting
  • amplify-libraries

Which platform(s) are affected by this PR (if applicable)?

  • JS
  • Swift
  • Android
  • Flutter
  • React Native

Checks

  • Does this PR conform to the styleguide?

  • Does this PR include filetypes other than markdown or images? Please add or update unit tests accordingly.

  • Are any files being deleted with this PR? If so, have the needed redirects been created?

  • Are all links in MDX files using the MDX link syntax rather than HTML link syntax?

When this PR is ready to merge, please check the box below

  • Ready to merge

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

copyWith treats a null argument as unchanged, so it cannot clear
a field. Point the Flutter DataStore and GraphQL update examples
at copyWithModelFieldValues.

Fixes aws-amplify/amplify-flutter#5019

@cadivus cadivus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AzazelSensei,
thanks for your PR! I have two comments about it.

Comment thread src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx Outdated
Comment thread src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx
Wrap the DataStore note in a Callout and keep oldPost/newPost.
@AzazelSensei

Copy link
Copy Markdown
Author

Applied the Callout and oldPost/newPost on the DataStore snippet, and added the same copyWithModelFieldValues note on the gen 2 Flutter mutate pages.

@cadivus

cadivus commented Aug 31, 2026

Copy link
Copy Markdown
Member

Hi @AzazelSensei,
thanks for the fixes! I found two more things when I had a second look:

Don't we have the second problem here? If so, I think

final memberWithRemovedTeam = existingMember.copyWith(team: null);
final memberRemoveRequest = ModelMutations.update(memberWithRemovedTeam);
final memberRemoveResponse = await Amplify.API.mutate(request: memberRemoveRequest).response;

should be

final memberWithRemovedTeam = existingMember.copyWithModelFieldValues(
  teamId: ModelFieldValue.value(null),
);
final memberRemoveRequest = ModelMutations.update(memberWithRemovedTeam);
final memberRemoveResponse = await Amplify.API.mutate(request: memberRemoveRequest).response;

and similar.

final cartWithRemovedCustomer = existingCart.copyWith(customer: null);
final cartRemoveRequest = ModelMutations.update(cartWithRemovedCustomer);
final cartRemoveResponse = await Amplify.API.mutate(request: cartRemoveRequest).response;

should be.

final cartWithRemovedCustomer = existingCart.copyWithModelFieldValues(
  customerId: ModelFieldValue.value(null),
);
final cartRemoveRequest = ModelMutations.update(cartWithRemovedCustomer);
final cartRemoveResponse = await Amplify.API.mutate(request: cartRemoveRequest).response;

copyWith(null) keeps the current value on Flutter models.
@AzazelSensei

Copy link
Copy Markdown
Author

Updated the two Flutter belongs-to unlink examples to copyWithModelFieldValues with ModelFieldValue.value(null).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants