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

Incorrect compilation error when adding optional arguments to multiple function arguments #3087

Open
ubeing opened this issue Nov 5, 2024 · 1 comment

Comments

@ubeing
Copy link

ubeing commented Nov 5, 2024

What happened?

"Expected to find ')'."

Steps to reproduce problem

Additional info

VScode Copilot repeated error correction is invalid, Cursor repeated error correction is invalid.

Machine

MacBook Pro 15"
Operating system:
macOS
Version:
15.1

Your code

What code was in the editor, if any, when the failure occurred? You
can paste it in below:

    String userInfo(String name,  {String gender = 'unknown'}, [int age = 0]) {
        return 'Hi, $name, gender: $gender, age: $age';
    }

DartPad's output

Did DartPad print anything to the console pane? If so, paste it below:

parameters.dart:1:58: Error: Expected ')' before this.
String userInfo(String name,  {String gender = 'unknown'}, [int age = 0]) {
                                                         ^
parameters.dart:2:45: Error: Undefined name 'age'.
  return 'Hi, $name, gender: $gender, age: $age';

If you were running Flutter code, you can also paste an image of the
Flutter output directly into this bug report.

@IldySilva
Copy link
Contributor

IldySilva commented Nov 28, 2024

@ubeing ,in dart ,positional optinal parameters ([]) and named optional parameters ({}) can't be mixed in the same parameter list.

Dart allow two kinds of optional parameters:

  • Named Optional Parameters: Enclosed in curly braces {} and usually assigned default values.
    Can be omitted during function calls by name.

  • Positional Optional Parameters: Enclosed in square brackets [] and ordered.
    Can be omitted during function calls by position

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

No branches or pull requests

2 participants