Skip to content

No analyzer warning or any other errors when I use a timer wrong with =>. #57669

Open
@apwilson

Description

@apwilson

I wrote the following code and was surprised when _updateTime was never called:

new Timer.periodic(
  const Duration(seconds: 1),
  (_) => _updateTime,
);

What the code should have looked like was the following:

new Timer.periodic(
  const Duration(seconds: 1),
  (_) => _updateTime(),
);

The surprising part about this is in the first code set I was returning a function instead of calling it which is invalid as the Timer callback is a void function but I received no run-time or analysis-time errors.

Why I'd expect an error is because this does generate an analysis error which I think is synonymous to the first code set:

new Timer.periodic(
  const Duration(seconds: 1),
  (_) {
    return _updateTime;
  },
);

_updateTime's signature looks like this:

void _updateTime() {
 // do stuff
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-requesttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions