Skip to content

Conversation

@ValentinVignal
Copy link
Contributor

Closes flutter/flutter#114406

This PR adds the support of default values in the route constructors.
If the URL/location doesn't have a set value for the field, it uses the default value. If the value of the field is equal to the default value, it doesn't include it in the location/URL.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

final String parameterName = param.name;
String condition = '$parameterName != null';
if (param.hasDefaultValue) {
condition += '&& $parameterName != ${param.defaultValueCode!}';
Copy link
Contributor

@chunhtai chunhtai Nov 30, 2022

Choose a reason for hiding this comment

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

How do you tell the difference between a parameter with default value but explicitly set to null? in that case the URL it generates will always represent parameter with default value

Copy link
Contributor Author

@ValentinVignal ValentinVignal Dec 1, 2022

Choose a reason for hiding this comment

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

I was considering the type being non-nullable when a default value is set. But you are right, this is not always true. I assume we could allow / need ?param=null in the URL
But this might conflict if param is a String?. Does ?param=null mean String? param = null or String? param = 'null' ?

I believe we can only support default parameters when the type is not nullable?

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, I can't think of other way to support nullable field without som caveats. Only allow non-nullable field to have default value SGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So what do you think about the current implementation? Do you want me to change something?

Copy link
Contributor

Choose a reason for hiding this comment

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

The more I think about this the more I think we probably shouldn't add this api. cc @johnpryan WDYT

Copy link
Contributor

Choose a reason for hiding this comment

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

you can handle the default value in build method.

If we really want to do this, we need some kind of warning or even linting error if developer assign null to the nullable parameter.

Copy link
Contributor Author

@ValentinVignal ValentinVignal Dec 17, 2022

Choose a reason for hiding this comment

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

If we really want to do this, we need some kind of warning or even linting error if developer assign null to the nullable parameter.

But didn't we say the parameter would be non-nullable? (#2826 (comment))?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes but what i meant was that we need something to warn developer about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can try to implement a warning or an error if the type is nullable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think about 🔊 Throw an error when a default value is used with a nullable type?

Now it throws an error if a default value is used with a nullable type

@ValentinVignal
Copy link
Contributor Author

@chunhtai Is there anything else you want me to do about this?

@stuartmorgan-g
Copy link
Collaborator

@chunhtai Ping from triage: what's the status of this review?

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

sorry for the late reply, just come back from vacation. I think this looks good now. Can you add update the readme to include this new functionality?

@ValentinVignal
Copy link
Contributor Author

@chunhtai I've modified the README in Update README. Tell me if that's good enough for you

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM

}
```

A query parameter equal to its default value is not included in the location.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
A query parameter equal to its default value is not included in the location.
A query parameter equals to its default value is not included in the location.

Copy link
Contributor Author

@ValentinVignal ValentinVignal Feb 2, 2023

Choose a reason for hiding this comment

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

Actually "equal" is not the verb in the sentence, the verb is "is"

"A query parameter equals to its default value" is the subject of the sentence.

So I guess it should be "equal" ?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah sorry, should it be this then? "A query parameter that equals to its default value"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I've changed it

@chunhtai chunhtai requested a review from hannah-hyj February 1, 2023 17:12
Copy link
Member

@hannah-hyj hannah-hyj left a comment

Choose a reason for hiding this comment

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

LGTM

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 8, 2023
@auto-submit auto-submit bot merged commit f2f6c5e into flutter:main Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: go_router_builder

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router_builder] Support default value for the route parameters

4 participants