1

I recently got into this problem.

Are the following Dart statements equal or else what are the differences:

class StaticConstVsFinal {
    static const timeout1 = const Duration(seconds: 5);  // ------> 1

    static const timeout2 = Duration(seconds: 5);        // ------> 2

    final timeout3 = const Duration(seconds: 5);         // ------> 3

    final timeout4 = Duration(seconds: 5);               // ------> 4
}

As per my understanding static const variables doesn't allocate memory no matter how many instances of StaticConstVsFinal are made, memory usage won't increase as those are class variables, but for final variables it increases memory usage. ---> Correct me if I'm wrong.

What are the pros and cons of defining these Flutter Widgets as constants, is that essential too?

Please explain someone the differencs, pros and cons of each way if possible.

I know there's a much similar Question here but it doesn't cover all the above mentioned scenarios. So, please don't mark these as a duplicate.

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
  • 1
    https://stackoverflow.com/questions/53492705/does-using-const-in-the-widget-tree-improve-performance – Yuri H Jul 13 '21 at 12:56

2 Answers2

3

@CopsOnRoad 's answer is good, but specifically with regard to Flutter, const widgets have a particular benefit:

When Flutter rebuilds your widget tree, it compares the old widget tree with the new one to see which parts need rebuilding. If it comes across a point where oldWidget == newWidget, it simply reuses the old widget's existing element at that point (i.e. it doesn't rebuild).

However, Widget.== currently uses identical, which means that one widget can only be used to update another if they are the same widget (i.e. referring to the same instance).

const gives you this for free. Any parts of your widget tree that are const are guaranteed to be the same across every rebuild, since there is only ever one instance. This effectively means that const widgets "block" the propagation of rebuilds.

Note that this is a implementation detail of the Flutter framework and is independent of any performance benefits from using const more generally

cameron1024
  • 9,083
  • 2
  • 16
  • 36
1

These two are equivalent.

static const timeout1 = const Duration(seconds: 5);  // ------> 1
static const timeout2 = Duration(seconds: 5); // ------> 2    

These are equivalent:

final timeout3 = const Duration(seconds: 5);         // ------> 3
const timeout3 =  Duration(seconds: 5);         // ------> 3a
var timeout3 = const Duration(seconds: 5);         // ------> 3b

When you use static, you're doing a lazy initialization which means unless the variable is used, it won't be initialized. The first two code snippets (timeout1 and timeout2) are doing that.

This one is odd one among others because it is not const.

final timeout4 = Duration(seconds: 5);               // ------> 4
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • Thanks for the explanation, especially the separating out the 4th one, and also showing alternatives of the 3rd one. Any Flutter specific explanations if we are to do the same for Flutter Widgets? – Randika Vishman Jul 13 '21 at 17:16
  • 1
    Try to use `const` as much as you can (for better performance) because using `const` ensures that you don't create a new instance of the underlying widget no matter how many times you instantiate the object. – CopsOnRoad Jul 13 '21 at 17:54
  • Yeah, true story! Thanks again. – Randika Vishman Jul 13 '21 at 17:55