aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.32k stars 247 forks source link

Document how to set a model field to null #5019

Open sampaio96 opened 4 months ago

sampaio96 commented 4 months ago

Description

The auto-generated model.dart code on Flutter does not allow setting nullable fields to null.

model = model.copyWith(nullableField: null) Does not work.

We need an alternative like model = model.removeKey(Model.NULLABLEFIELD)

Categories

Steps to Reproduce

Take this model:

type Model @model {
  id: ID!
  nullableField: String
}

This is the auto-generated Model.dart, which we use to update the model.

Model copyWith({String? nullableField}) {
    return Model._internal(
      id: id,
      nullableField: nullableField ?? this.nullableField,
    )
}

If you run:

model = model.copyWith(nullableField: null)

Your model will not change the nullableField to null.

Screenshots

No response

Platforms

Flutter Version

3.22.0

Amplify Flutter Version

1.7.0

Deployment Method

Amplify CLI

Schema

type Model @model {
  id: ID!
  nullableField: String
}
Jordan-Nelson commented 4 months ago

HI @sampaio96 - You can use model.copyWithModelFieldValues() for this. See example here: https://github.com/aws-amplify/amplify-flutter/issues/1480#issuecomment-1589763058.

I am going to mark this as a docs issue since it doesn't appear to be shown in the documentation.

sampaio96 commented 4 months ago

Thank you! Perfect.

NikaHsn commented 4 months ago

thanks for opening this issue, we are working to improve our documentation.