final and const keywords | Flutter
1 min readFeb 23, 2023
final
- “final” variables are initialized only once and can’t be reassigned.
- Declaring type boosts performance.
- “final” methods and classes, compilation error.
- “final+const” creates constants.
- There is no “val” keyword in Flutter, the equivalent keyword is “final”.
class Button {
final String title;
const Button({required this.title});
void push() { }
}
const
- “const” in Flutter declares constant variables evaluated at compile-time.
- Cannot be changed.
- Used with variables and constructors, not classes.
- Improves performance by enabling compiler optimizations.
const int myInt = 42;
const button = Button(title: ‘Hello!!’);
button.push();
Hey, fellow mobile lovers! Are you looking for a channel that’s as passionate about mobile development as you are? My channel is the place to be! With a focus on all things mobile, from app development to design, I’m sure you’ll find something to love. So why not join my community of like-minded individuals and let’s create something amazing together? Hit that follow button and let’s get started!