0

This is a duplicate of this question: dart: wrap all function calls.

I've just begun to learn what mirrors are and given that I still can't comment on any post, I figured I should just ask it here as a question on its own.

His code went like this:

class Wrapper{
  _wrap(Function f, Symbol s){
    var name = MirrorSystem.getName(s);
    print('Entering $name');
    var result = f();
    print('Leaving $name');
    return result;
  }
}

@proxy
class StoryTellerProxy extends Wrapper implements StoryTeller{
  final InstanceMirror mirror;

  StoryTellerProxy(StoryTeller storyTeller): mirror = reflect(storyTeller);

  @override
  noSuchMethod(Invocation invocation) =>
      _wrap(() => mirror.delegate(invocation), invocation.memberName);
}

I was wondering if using mirrors would render a large increase in my codebase size. I was hoping to apply this to my API handler class to essentially wrap all function calls to the API, catching errors and responses without 200/201 as status codes.

Louise
  • 31
  • 4
  • 2
    You can't use `mirrors` in Flutter, so perhaps the question is moot. – Richard Heap Jun 01 '22 at 14:31
  • oh.. when are they ever used? Actually I was hoping I could catch all errors in my app and have it sent to my email, not just ones in my API handler – Louise Jun 01 '22 at 15:19
  • Barely ever any more, as not compatible with AOT compilation and tree-shaking. – Richard Heap Jun 01 '22 at 15:29
  • If you want to catch all uncaught exceptions, try having your application do all of its work within a callback to [`runZoneGuarded`](https://api.dart.dev/stable/dart-async/runZonedGuarded.html). – jamesdlin Jun 01 '22 at 16:21
  • I'll try doing just that, thanks! – Louise Jun 01 '22 at 22:28

0 Answers0