1

If I declare that in Dart : final Color _iconColor = Color.fromARGB(210, 198, 15, 1, Vs Code display this message : Prefer const with constant constructors.

So the result is : final Color _iconColor = const Color.fromARGB(210, 198, 15, 1);. And VsCode is good.

But if my variable _iconColor is already final, why add const inside ?

benbd5
  • 15
  • 3

1 Answers1

3

const and final are not the same.

const means that the value is known at compile time, whereas final means that the variable is immutable after being set.

From Flutter best practices here:

Use const constructors on widgets as much as possible, since they allow Flutter to short-circuit most of the rebuild work

Using const keyword everytime it's possible will increase your performances, since Flutter won't have to re-build const widgets.

In your case, you define your _iconColor as final, which means that you won't be able to modify it afterwards. And you set it to a const value, known at compile time.

lucie
  • 895
  • 5
  • 19