1

I'm really bothering with the "const" in Flutter 3.0.

Why can't Flutter make "const" default automatically?

Since during editing, it already displays warning about a certain expression having to be "const", it could automatically infer that expression as "const".

Without us having to pollute the code by filling it with "const". The same way he did with "new" in version 2.0.

Is there any impediment for this to be so? I can't imagine any, but if there were, a keyword could be created to say the opposite of "const". It could be the "var", for example.

sérgio brito
  • 388
  • 3
  • 6
  • Because we modify the code. – Md. Yeasin Sheikh Jul 03 '22 at 14:45
  • 2
    https://github.com/dart-lang/language/issues/1410#issuecomment-1121120727 – mmcdon20 Jul 03 '22 at 14:51
  • Does this answer your question? [Fix all const warning flutter](https://stackoverflow.com/questions/68972127/fix-all-const-warning-flutter) – Md. Yeasin Sheikh Jul 03 '22 at 15:05
  • 1
    Automatically [making everything `const` can affect your program behavior](https://stackoverflow.com/a/57618161/). It's not something that should just be done arbitrarily. – jamesdlin Jul 03 '22 at 18:29
  • But the compiler in Flutter 3.0 is showing warnings, forcing us to use "const". This way, if we don't want to use meta-commands to turn off lint, we're forced to put everything as "const". So we're not really having a choice. Either we put everything as "const" or we have to use meta-commands to turn off these warnings. – sérgio brito Jul 03 '22 at 22:55
  • @YeasinSheikh, Isn't this actually the same thing I'm proposing? But a tool will be filling my code with "const". Without me seeing where. What I'm proposing is that it be transparent to the developer. And we create another keyword for those who want/need a different behavior. Which I think must be the immense minority. – sérgio brito Jul 03 '22 at 23:02
  • I think it you are trying to say *suggesting us to use "const".* Instead of *forcing us to use "const".* . Maybe we will get in future btw putting manually const isn't bad though, like we are learning something, instead of depending on machine. – Md. Yeasin Sheikh Jul 03 '22 at 23:07
  • As I get a warning on many of the classes I'm creating, I'm being forced to use "const". Because if I don't use it, my program will be filled with warnings, making it impossible to detect those warnings that are really important. – sérgio brito Jul 05 '22 at 13:10
  • I think this is a great question. The reason it's a good question is because it isn't obvious there wouldn't be an automatic way to deal with const. Helping the compiler optimize is a mundane task best done by the machine, so my instinct is that every mundane task has an automated solution. Following that instinct I arrived here and found this discussion. Whatever you think about how const is handled, I for one thank you sérgio brito for asking such a good question. – Chris Nadovich Mar 16 '23 at 00:22

1 Answers1

1

We have option to set const automatically from vs-code setting.

enter image description here

You can check original question on Fix all const warning flutter

Do I like it? No, think about column widget, where children can be const. After writing some children, you find that everything is const . This situation automatic system will provide const before list

  Column(
    children: const [

Now we have added another child which is not const, we need to move at the top and remove the const, then it will auto apply const inner child. Scrolling mouse lose focus of keyboard and cost good amount of time.

As @mmcdon20 included git issue on Compiler should try to make everything const.

You can check the last comment where they mentioned

For history, during the Dart 2.0 language design, we considered automatically making all expressions that could be constant also be constant. Examples included things like Duration(seconds: 1), which might as well be constant.
The reason we ended up not doing so was that the failure modes were scary, and that it wasn't clear how to handle list/map literals.
If you write MyPotentiallyConstantClass(<int>[1]), then that expression can be constant, but it changes the behavior of the list if it is. You get an immutable list, and then there would be syntax to ask that list to be non-constant. We might have to introduce MyPotentiallyConstantClass(new <int>[1]). Even worse, almost every list and map literal seemed like it would default to constant (because it could), and most of those are actually intended to be mutable.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • The result of this "dart fix" Isn't it actually the same thing I'm proposing? But a tool will be filling my code with "const". Without me seeing where. What I'm proposing is that it be transparent to the developer. And we create another keyword for those who want/need a different behavior. Which I think must be the immense minority. – sérgio brito Jul 03 '22 at 23:09
  • I think you are proposing a feature, and it might be better to request on GitHub, you can connect with that git issue . Also check [this answer from jamesdlin](https://stackoverflow.com/a/57618161/10157127) – Md. Yeasin Sheikh Jul 03 '22 at 23:11
  • The Flutter code is becoming full of "const". Its becoming dirty. All to "solve" performance issues for a minority of applications. Let's create a word to negate the "const" and make it default, automatic and transparent. – sérgio brito Jul 03 '22 at 23:12
  • Sorry, but I can't tell & I am ok with it. You can check on [github](https://github.com/flutter/flutter) – Md. Yeasin Sheikh Jul 03 '22 at 23:18