In Dart, const is used to declare compile-time constants. Constants declared with const are evaluated at compile-time, allowing Dart to optimize their usage. Here are a few key points about const in Dart:
const int x = 5;
const String name = 'John';
const int x = 5;
// This will cause a compilation error
x = 10;
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
void main() {
const p1 = Point(1, 2);
// This will cause a compilation error
p1.x = 5;
}
const List< int> numbers = [1, 2, 3];
const Set< String> names = {'Alice', 'Bob', 'Charlie'};
const Map< String, int> scores = {'Alice': 100, 'Bob': 90, 'Charlie': 80};