PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

[FIXED] Why use const sometimes when instantiating a widget in Flutter?

 April 27, 2022     flutter, intellij-idea, lint, warnings     No comments   

Issue

I have this widget in Flutter that represents a simple loading:

class Loading extends StatelessWidget {
  const Loading({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

I'm using IntelliJ on version 2021.2.1 and I've been getting this warning at various times while building code in flutter:

enter image description here

To solve this lint problem you can just add the reserved word const after the return statement, leaving the code as follows:

class Loading extends StatelessWidget {
  const Loading({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const Material(
      child: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

How does this really change to flutter? Is it a significant change? If not very significant is there a way to disable these warnings in IntelliJ? Because I think them quite boring.


Solution

It will prevent the Widget from being rebuild when its parent is rebuilt, as you already know that it will not change ever. Thus, only needing to build it once, you gain performance by making it a const.



Answered By - Tim Jacobs
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing