How to listen to flutter application console output?

221

One possible solution that I can think of is to intercept print() method and then you can get all the values whenever the print() method is called in the current zone.

After intercepting the value, you can save it to a file or so whatever you want to. I don't know if it's the most suitable way to do it.

  void main() {
  runZoned(() {
    // Ends up printing: "Intercepted: in zone".
    runApp(MyApp());
  }, zoneSpecification: new ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
    parent.print(zone, "Intercepted: $line");
    //save to a file or do whatever you want
  }));
}
Share:
221
Abozanona
Author by

Abozanona

Hi Everyone, I'm abozanona! I work as a team lead for Stellantis (a leading global automaker and provider of innovative mobility solutions.

Updated on December 27, 2022

Comments

  • Abozanona
    Abozanona over 1 year

    I have different libraries added to my flutter application. I'm implementing a feature in the application where whenever an error or message is printed to the console from any library or error message, I want to send that error or message string to the server.

    How can I listen to console in flutter programmatically so whenever a string is printed to the console I can capture it to send it to the server later?