Closed
Description
Observed result
After calling a method with name on
after a try-catch block, there is the following compilation error:
macOS:
The name '' isn't a type and can't be used in an on-catch clause.
Dartpad:
Error: Expected a type, but got '('.
on();
^
Expected result
Either of those:
- the
on
method call should not be interpreted as part of the try-catch block theon
keyword should not be allowed as function or method name- the analyzer would not issue an "Avoid empty statements." warning for a semicolon after the try-catch block, because the semicolon is required
Steps to reproduce
void main() {
try {
print('Trying dangerous things');
} catch(e) {
print('Caught exception');
}
// ; // Workaround
on();
}
void on() {
print('"on" method called');
}
Environment
- Tested with Dart version 2.14.4
- This issue could be reproduced with Linux, macOS, and Dartpad (Firefox)
Diagnostic information
The problem was found in this kind of code, using the BloC library :
class ScheduleBloc extends Bloc<ScheduleEvent, ScheduleState> {
//...
ScheduleBloc()
: super(
// ...
) {
try {
_clockSubscription = _clock.tick().listen((event) {
add(ClockTicked());
});
} catch (e) {
print("Hello");
}
; // Adding semicolon as workaround
on<ClockTicked>((event, emit) {
emit(
// ...
);
});
}
}