Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test/debug tree shaking in Flutter?

Tags:

flutter

dart

Flutter comes with tree shaking compilation. Including only the code used.

But this is easily breakable inadvertently.

Is there any way to test and debug tree shaking?

like image 515
Rémi Rousselet Avatar asked Oct 15 '18 18:10

Rémi Rousselet


People also ask

Does flutter have tree shaking?

Tree Shaking is also used in Flutter to reduce the final packet size. Flutter provides three construction modes. For each mode, the Flutter compiler has different optimizations on the output binary files. The Tree Shaking mechanism can't be triggered in the Debug mode.

What is kDebugMode in flutter?

kDebugMode top-level constant Null safety bool const kDebugMode. A constant that is true if the application was compiled in debug mode. More specifically, this is a constant that is true if the application was not compiled with '-Ddart. vm.


1 Answers

For debugging, it is possible to connect to the Observatory in profile mode. Then inspect the content of desired dart file

You won't see the actual sources in profile mode; but you'll see an overview of what is inside the file, including the defined classes and their methods.

For example, a widget such a the following:

class Home extends StatelessWidget {
  _unused() {
    print('home');
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

when inspected in the Observatory in profile mode; _unused method will not be in the function list:

enter image description here

like image 117
Rémi Rousselet Avatar answered Sep 19 '22 18:09

Rémi Rousselet