Manipulate StackTrace in dart

486

I ended up getting a reliable solution.

I used the stack_trace library and its provided classes Trace and Frame.

In the following example, trace.frames returns an imutable list, so I perform a deep copy. I am ok with this as it only runs on a crash anyways.

    StackTrace stacktrace = StackTrace.current;
    Trace trace = Trace.from(stacktrace);
    List<Frame> frames = trace.frames;
    List<Frame> newFrames = List<Frame>();

    // start at index 1 to omit frame #0
    for (int i = 1; i <  frames.length; i++) {
        Frame f = frames[i];
        newFrames.add(Frame(f.uri , f.line , f.column , f.member));
    }

    Trace newTrace = Trace(newFrames);
Share:
486
Scorb
Author by

Scorb

Updated on December 21, 2022

Comments

  • Scorb
    Scorb over 1 year

    Summary: I have a stacktrace in dart, where I want to remove frame #0, and then have the whole stacktrace adjust (frame #1 is now frame #0, frame #2 is now frame #1).

    Details: I have written my own assert type function in dart, that if fails, will grab the current stacktrace and send to crashlytics.

    static void asrt(bool condition)
    {
        if (condition)
            return;
        StackTrace stacktrace = StackTrace.current;
        Crashlytics.instance.recordError("assert triggered" , stacktrace);
    }
    

    The problem is that crashlytics identifies each error based on the first stackframe. So all of my errors are being identified as that same error, because I am grabbing the stacktrace from the same method. I understand I could pass the stacktrace from the caller, but a preferrable solution to me would be to manipulate the stacktrace so the caller does less work.

    Is this doable in dart?