Open
Description
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
}