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..e989e7db905 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,15 @@ Future updatePost() async { } } ``` + + + +`copyWith` cannot assign `null` to a nullable field: a null argument keeps the current value. Use `copyWithModelFieldValues` instead: + + + +```dart +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 3472dce68f8..a6de33eefc0 100644 --- a/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx +++ b/src/fragments/lib/graphqlapi/flutter/mutate-data.mdx @@ -18,6 +18,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'); +} +``` + To delete the `Todo`: ```dart 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; ``` 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