Skip to main content

Command Palette

Search for a command to run...

Fix Flutter const with const constructors

Mobile Development with Google Flutter Framework

Updated
1 min read
Fix Flutter const with const constructors
P

Hi, I am a technology enthusiast and lover of the cloud.

As a designer (brand identity and web design) I am a perfectionist to minor pixel

So you are busy hammering away at your Dart code but, you keep seeing these squiggly lines: alt 'squiggly blue lines on code'

The problems terminal in Visual Studio Code might be showing this alt 'Prefer const with constant constructors.' To fix it, open the analysis_options.yaml which contains the linter rules. Add this line after the rules prefer_const_constructors: false

Your final lint rule should look like this:

linter:

  rules:

    prefer_const_constructors: false

Solution view

run the command dart fix --apply in the terminal

alt 'dart fix doing it's job' and voila alt 'linting issue fixed'

What is the purpose of linting?

Linting defines the rules on how your code will be structured. These include rules like whether to use double quotes or single quotes or the spacing required after elements. My first encounter with it was working with Tailwind.css on a node.js tutorial. It is a pain in the neck! Hopefully, if you encounter this, now you have the solution. 😊

Take Note!

You might have situations where you need to prefer const constructors. It saves up memory allocation which in turn means better performance (in the longer run). A great explanation is provided on StackOverflow in detail.

M
MUSLMASAN4y ago

Thank you

1
A
AP4y ago

Petrus Nauyoma using const for widgets that are not changed or re-rendered over time is necessary for app improvement.

1
P

Well noted. I'm quite new to the framework. So when I am building smaller projects (as I am at the moment) it was a hindrance instead. Thanks for the info.

1

More from this blog

Peter Chronicles

3 posts

Hi, I am a technology enthusiast and lover of the cloud.

As a designer (brand identity and web design) I am a perfectionist to minor pixel levels. I love creating beautiful and functional web sites.