Reputation: 1398
Actually I am very much confused about this statement in dart and its (_) when does this required and what's difference with () ...What document should I read to make this concept clear like
TextField(
onSubmitted: (_) {
onsubmit();
},
Upvotes: 0
Views: 36
Reputation: 119
The onSubmitted callback function provides the submitted String in the text field as a value, so you can work with it and process it further in the function.
TextField(
onSubmitted: (String value) { print("Submitted value: $value"); },
),
If you for whatever reason don't need this value, because you do not use it in the function, you can replace the String value
with a _
, so anyone who reads the code immediately knows that this value is not used.
You can't just leave it out like this:
TextField(
onSubmitted: () { print("Doing something"); },
),
because you will get the error:
The argument type 'void Function()' can't be assigned to the parameter type 'void Function(String)?'.
So you just assign the value to this variable called _
, which you are never going to use.
TextField(
onSubmitted: (_) { print("Doing something"); },
),
Relevant section in the "Effective Dart" Guide
Upvotes: 2