Search code examples
dartreflection

How to get the name of the current and calling function in dart?


C# has:

System.Reflection.MethodBase.GetCurrentMethod().Name

Does Dart have something similar but returns results for both the function that is currently being run as well as the name of the function that called the currently run function.


Solution

  • import 'dart:mirrors';
    ...
    MethodMirror methodMirror = reflect(functionOne).function;
    

    See also https://github.com/dart-lang/sdk/issues/11916#issuecomment-108381556

    This will only work in the Dart command line VM, but not in the browser or Flutter because there reflection is not supported.

    Code generation solutions like https://pub.dartlang.org/packages/reflectable might work instead where reflection is not available.

    https://github.com/dart-lang/sdk/issues/28372 seems related.