Wednesday, May 31, 2023

Understanding the Use of Underscore (_) Parameters in Dart

Understanding the Use of Underscore (_) in Dart

In Dart programming, you might have noticed that parameters sometimes include an underscore (_), as seen in doSomething(_) => something. This specific notation indicates that the parameter is not being used.

Dart Style Guide on Unused Parameters

The Dart style guide provides a recommendation for handling unused callback parameters:

"Prefer using _, __, etc. for unused callback parameters."

"If a callback parameter isn't required, it can be named as _, __, or ___. This convention signifies that the value is not utilized."

Best Practices for Unused Callback Parameters in Dart

In essence, it's good practice to name your unused callback parameters as _, __, or ___. This universally accepted method signals that the value is not being used.

An Example of Unused Parameter in Dart Code

To illustrate this concept, consider the following code snippet where the second parameter of the function doSomething is not utilized:


void doSomething(int a, _) {
  print(a);
}

0 개의 댓글:

Post a Comment