Use a function declaration to bind a function to a name
Asked Answered
G

1

7

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);
    };
Gine answered 3/11, 2021 at 11:43 Comment(1)
I believe the warning comes from this line final onChanged = (CameraDescription? description) {, which should be onChanged(CameraDescription? description) {, here is the specific rule dart-lang.github.io/linter/lints/…Ajmer
E
12

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

Erleneerlewine answered 3/11, 2021 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.