Dart – is there a way to prevent adding elements to immutable list at compile time?

Solution for Dart – is there a way to prevent adding elements to immutable list at compile time?
is Given Below:

I am learning Dart.

I have a code like below:

void main() {
  someMethod();
}

void someMethod() {
  const List<String> strings = ["First", "Second"];
  strings.add("Third");
}

I would like to be forced by the IDE and compiler (for example by the underline in red in the editor) to do such things (so I cannot have exception at runtime)

Is there something that I am missing?

Unfortunately, no. Dart does not support immutable lists at the type system level.

However, some packages provide this:

built_collection (which is often used with built_value) allows you to create deeply immutable data structures. For example:

final list = BuiltList([1, 2, 3]);
list.add(4);  // compile time error, there is no add() method
final newList = list.rebuild((b) => b.add(4));

print(list);     // prints [1, 2, 3]
print(newList);  // prints [1, 2, 3, 4]

However, since BuiltList doesn’t have an add method, it cannot implement List. However, if you need to pass it to a function that requires a List, you can just use toList().

You also lose the ability to use const, but in Flutter (except when dealing with Widgets, the performance impact is fairly small.