Yonkee
Yonkee

Reputation: 1883

Flutter const with const constructors

Up until today I haven't seen this dart code suggestions. I am happy to follow best practise, but to be honest, this makes no sense showing up in a stateful widget with no constructor. I thought it might be related to the @immutable annotation, but it doesn't seem to be and the dart documentation doesn't really help.

Dart Docs https://dart-lang.github.io/linter/lints/prefer_const_constructors.html

Code Suggestions in VSCode

Prefer const literals as parameters of constructors on @immutable classes.dart. || Prefer const with constant constructors

Question: Is this something I need to care about or has my plugin on VSCode gone haywire?

Code sample where this shows for all widgets.

   Column(
            children: [
              Container(
                margin: EdgeInsets.only(left: 20, right: 20),
                height: 50,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(
                    Radius.circular(30),
                  ),
                  color: Colors.black,
                ),
                child: Center(
                  child: Text(
                    'Create Account',
                    style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.w600,
                        fontSize: 19),
                  ),
                ),
              ),
              SizedBox(
                height: 20,
              )

Screen for completeness

enter image description here

Upvotes: 5

Views: 6808

Answers (2)

Kalpesh Khandla
Kalpesh Khandla

Reputation: 736

To avoid the prefer const with constant constructors warning add this rule prefer_const_constructors : false to the analysis_options.yaml file.

linter:
rules:
prefer_const_constructors : false
# avoid_print: false  # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true  # Uncomment to enable the 

Upvotes: 4

Jay Mungara
Jay Mungara

Reputation: 7148

Lint interpreter always will show you the best possible way to improvise your coding and performance optimization tips.

For ex: in your code, there are many widgets that are not going to rebuild. cause it's compile-time constants as they are not dependent on other dynamic properties.

Column(
                children: [
                  Container(
                    margin: const EdgeInsets.only(left: 20, right: 20),
                        height: 50,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(
                            Radius.circular(30),
                          ),
                          color: Colors.black,
                        ),
                        child: const Center( // compile time constant widget
                          child: const Text( // compile time constant widget
                            'Create Account',
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.w600,
                                fontSize: 19),
                          ),
                        ),
                      ),
                      const SizedBox( // compile time constant widget
                        height: 20,
                      )

Let say, your Text widget value depends on some other variable x, instead of the static value "Create Account" Then you cannot add const keyword before your Text Widget as it's not compile-time constant value.

Even if you don't worry about this kind of lint suggestions, it's fine. But, for your long journey in the development will be very smoother if you take consideration of lint suggestions

Upvotes: 8

Related Questions