From 72cdeb75ae55350bc0cd6be6167fa5e9c8b212ac Mon Sep 17 00:00:00 2001 From: Abdullah <89297042+AzazelSensei@users.noreply.github.com> Date: Thu, 27 Aug 2026 12:30:48 +0000 Subject: [PATCH 1/3] Document copyWithModelFieldValues for nullable fields 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 --- .../flutter/data-access/update-snippet.mdx | 8 ++++++++ .../lib/graphqlapi/flutter/mutate-data.mdx | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx b/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx index bebac670813..8a5365e5f6b 100644 --- a/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx +++ b/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx @@ -19,3 +19,11 @@ Future updatePost() async { } } ``` + +`copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + +```dart +final updated = existing.copyWithModelFieldValues( + content: ModelFieldValue.value(null), +); +``` diff --git a/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx b/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx index 3472dce68f8..98ed6ebd8cd 100644 --- a/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx +++ b/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx @@ -18,6 +18,20 @@ Future updateTodo(Todo originalTodo) async { } ``` +`copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + +```dart +Future clearTodoDescription(Todo originalTodo) async { + final cleared = originalTodo.copyWithModelFieldValues( + description: ModelFieldValue.value(null), + ); + + final request = ModelMutations.update(cleared); + final response = await Amplify.API.mutate(request: request).response; + safePrint('Response: $response'); +} +``` + To delete the `Todo`: ```dart From 38c84cb4e2605a24cdbd9675a64a861622255e23 Mon Sep 17 00:00:00 2001 From: Abdullah <89297042+AzazelSensei@users.noreply.github.com> Date: Sun, 30 Aug 2026 00:51:42 +0000 Subject: [PATCH 2/3] Document copyWithModelFieldValues on gen 2 mutate pages Wrap the DataStore note in a Callout and keep oldPost/newPost. --- .../flutter/data-access/update-snippet.mdx | 6 +++++- .../lib/graphqlapi/flutter/mutate-data.mdx | 4 ++++ .../build-a-backend/data/mutate-data/index.mdx | 18 ++++++++++++++++++ .../frontend/data/mutate-data/index.mdx | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx b/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx index 8a5365e5f6b..e989e7db905 100644 --- a/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx +++ b/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx @@ -20,10 +20,14 @@ Future updatePost() async { } ``` + + `copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + + ```dart -final updated = existing.copyWithModelFieldValues( +final newPost = oldPost.copyWithModelFieldValues( content: ModelFieldValue.value(null), ); ``` diff --git a/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx b/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx index 98ed6ebd8cd..a6de33eefc0 100644 --- a/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx +++ b/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx @@ -18,8 +18,12 @@ Future updateTodo(Todo originalTodo) async { } ``` + + `copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + + ```dart Future clearTodoDescription(Todo originalTodo) async { final cleared = originalTodo.copyWithModelFieldValues( diff --git a/src/pages/[platform]/build-a-backend/data/mutate-data/index.mdx b/src/pages/[platform]/build-a-backend/data/mutate-data/index.mdx index 53ca2a1fc8d..0da3662f09c 100644 --- a/src/pages/[platform]/build-a-backend/data/mutate-data/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/mutate-data/index.mdx @@ -293,6 +293,24 @@ Future updateTodo(Todo originalTodo) async { } ``` + + +`copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + + + +```dart +Future clearTodoDescription(Todo originalTodo) async { + final cleared = originalTodo.copyWithModelFieldValues( + description: ModelFieldValue.value(null), + ); + + final request = ModelMutations.update(cleared); + final response = await Amplify.API.mutate(request: request).response; + safePrint('Response: $response'); +} +``` + ## Delete an item diff --git a/src/pages/[platform]/frontend/data/mutate-data/index.mdx b/src/pages/[platform]/frontend/data/mutate-data/index.mdx index dc201477329..67b72d189ad 100644 --- a/src/pages/[platform]/frontend/data/mutate-data/index.mdx +++ b/src/pages/[platform]/frontend/data/mutate-data/index.mdx @@ -293,6 +293,24 @@ Future updateTodo(Todo originalTodo) async { } ``` + + +`copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + + + +```dart +Future clearTodoDescription(Todo originalTodo) async { + final cleared = originalTodo.copyWithModelFieldValues( + description: ModelFieldValue.value(null), + ); + + final request = ModelMutations.update(cleared); + final response = await Amplify.API.mutate(request: request).response; + safePrint('Response: $response'); +} +``` + ## Delete an item From 3d91e6c6475f44f2c579d020fca541981ffae4ad Mon Sep 17 00:00:00 2001 From: Abdullah <89297042+AzazelSensei@users.noreply.github.com> Date: Tue, 1 Sep 2026 09:33:29 +0000 Subject: [PATCH 3/3] Document copyWithModelFieldValues when clearing belongs-to links copyWith(null) keeps the current value on Flutter models. --- .../data/data-modeling/relationships/index.mdx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx index f36b8f92d7d..bb380a78187 100644 --- a/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/data-modeling/relationships/index.mdx @@ -263,7 +263,9 @@ do { ```dart -final memberWithRemovedTeam = existingMember.copyWith(team: null); +final memberWithRemovedTeam = existingMember.copyWithModelFieldValues( + teamId: ModelFieldValue.value(null), +); final memberRemoveRequest = ModelMutations.update(memberWithRemovedTeam); final memberRemoveResponse = await Amplify.API.mutate(request: memberRemoveRequest).response; ``` @@ -687,7 +689,9 @@ do { ```dart -final cartWithRemovedCustomer = existingCart.copyWith(customer: null); +final cartWithRemovedCustomer = existingCart.copyWithModelFieldValues( + customerId: ModelFieldValue.value(null), +); final cartRemoveRequest = ModelMutations.update(cartWithRemovedCustomer); final cartRemoveResponse = await Amplify.API.mutate(request: cartRemoveRequest).response; ```