Skip to content

Cannot call method with name 'on' after try-catch block #47541

Closed
@jmewes

Description

@jmewes

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
  • the on 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');
}

Try it on Dartpad

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(
        // ...
      );
    });
  }
}

Metadata

Metadata

Assignees

Labels

P3A lower priority bug or feature requestfront-end-fastalegacy-area-front-endLegacy: Use area-dart-model instead.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions