0

I am using optional parameters in Flutter, when i use named arguments require keyword is allowed however when i use optional parameters its gives an error.

The code below complains that the parameter is non-nullable and either add a require keyword When i add the require keyword it still complains

  TextFormField _textFormBuilder([required String label, required String hint]) {
    return TextFormField(
      keyboardType: TextInputType.text,
      textInputAction: TextInputAction.next,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        labelText: 'label',
        hintText: 'hint',
      ),
    );
  }

This code below works as expected:

  TextFormField _textFormBuilder({required String label, required String hint}) {
    return TextFormField(
      keyboardType: TextInputType.text,
      textInputAction: TextInputAction.next,
      decoration: InputDecoration(
        border: OutlineInputBorder(),
        labelText: 'label',
        hintText: 'hint',
      ),
    );
  }

Any ideas?

Solen Dogan
  • 139
  • 8

2 Answers2

0

[] means that the parameter is optional. If you add word required, it has no idea what do you want.

 TextFormField _textFormBuilder(String label, String hint)

The {} means they are named parameters.

Read about parameters here: What is the difference between named and positional parameters in Dart?

stacktrace2234
  • 1,172
  • 1
  • 12
  • 22
0

Optional parameters in Dart are optional and they are not supposed to be required which makes good-sense. Unlike named arguments they can be required!

Solen Dogan
  • 139
  • 8