Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a function declaration to bind a function to a name

I am new to flutter. I was using the camera plugin to add the camera to my app. And I got this warning Use a function declaration to bind a function to a name. How can I solve this one?

Code -

Widget _cameraTogglesRowWidget() {
    final List<Widget> toggles = <Widget>[];

    final onChanged = (CameraDescription? description) {
      if (description == null) {
        return;
      }

      onNewCameraSelected(description);
    };
like image 859
Maahdi.Codes Avatar asked Jul 21 '26 09:07

Maahdi.Codes


1 Answers

Dart's effective usage documentation suggests it is a better practice to use function declaration construct than it is to use assignment to an anonymous function.

That is

int foo() =>  1

instead of

int Function() foo = () => 1

In your example, that would mean declaring a function onChanged, instead of performing an assignment to onChanged:

void onChanged(CameraDescription? description){
  ...body
}

The reason you're seeing this warning is that since Flutter 2.3.0 flutter_lints is by default packaged along with any flutter applications.

For more on Dart lints and effective usage, see the documentation on Effective Dart: Usage and List of Dart lints. Specifically, the warning you see is found on prefer_function_declarations_over_variables

like image 92
Gabriele Boas Avatar answered Jul 23 '26 00:07

Gabriele Boas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!