Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using const in the widget tree improve performance?

Tags:

flutter

dart

When creating a widget tree, will inserting const before static widgets improve performance?

ie

child: const Text('This is some text'); 

vs

child: Text('This is some text'); 

I know that, with Dart 2, const is optional and will be inserted automatically is some places. Is this one of those situations? If it isn't, will using const reduce memory usage/improve performance?

Thanks for your answers!

like image 422
Noah Klayman Avatar asked Nov 27 '18 04:11

Noah Klayman


People also ask

Why is the const important in Flutter?

The const keyword is used when the value of the variable is known at compile-time and never changes. In other words, the compiler knows in advance what value is to be stored in that variable.

What is const constructor in Flutter?

A const constructor is an optimization! The compiler makes the object immutable, allocating the same portion of memory for all Text('Hi!') objects. But not Text(Math. random()) though, as its value can't be determined at compile time!

What is widget tree in Flutter?

A Flutter app is represented by a widget tree, similar to how the DOM on the browser is a tree structure. The widget tree is an actual tree data structure in code built behind the scenes by Flutter, but it's also a useful way to talk about the structure of your Flutter app.


2 Answers

It is a small performance improvement, but it can add up in larger apps or apps where the view is rebuilt often for example because of animations.
const reduces the required work for the Garbage Collector.

You can enable some linter rules in analysis_options.yaml that tell you when you should add const because it's not inferred but would be possible like

  • http://dart-lang.github.io/linter/lints/prefer_const_constructors.html
  • http://dart-lang.github.io/linter/lints/prefer_const_declarations.html
  • http://dart-lang.github.io/linter/lints/prefer_const_literals_to_create_immutables.html

or that reminds you when you use const but it is inferred anyway

  • http://dart-lang.github.io/linter/lints/unnecessary_const.html

See also https://www.dartlang.org/guides/language/analysis-options

like image 156
Günter Zöchbauer Avatar answered Oct 06 '22 00:10

Günter Zöchbauer


In the case of Flutter, the real gain with const is not having less instantiation. Flutter has a special treatment for when the instance of a widget doesn't change: it doesn't rebuild them.

Consider the following:

Foo(   child: const Bar(     child: Baz()    ), ) 

In the case of build method being called again (setState, parent rebuild, Inheritedwidget...), then due to the const for Bar subtree, only Foo will see its build method called.

Bar will never get rebuilt because of its parent, because Flutter knows that since the widget instance didn't change, there's nothing to update.

like image 42
Rémi Rousselet Avatar answered Oct 06 '22 00:10

Rémi Rousselet