Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Returning or NOT Returning a nullable from deserializeWith ? #1168

Open
fvisticot opened this issue Jun 18, 2022 · 1 comment
Open
Assignees
Labels

Comments

@fvisticot
Copy link

My code is implementing a fromJson method calling the deserializeWith method returning an optional.

  • Is it safe for me to return a Beacon and not a Beacon? ?

  • In which case a null will be returned from the deserializeWith method

Code would be simple to maintain if method is returning a non nullable value and throw exception when required

static Beacon? fromJson(String jsonString) {
    return serializers.deserializeWith(
        Beacon.serializer, json.decode(jsonString));
  }
@davidmorgan davidmorgan self-assigned this Jun 20, 2022
@davidmorgan
Copy link
Collaborator

That depends on the JSON.

Will it ever be the literal null? If so, deserialize can return null, and your method must return Beacon?.

If not, you should add a ! to the return statement and return Beacon. Then it will throw if the JSON says null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants