Skip to main content

Flutter Use 'const' with the constructor to improve performance Error Fix

Hello friends, For past few days I am seeing a improvement suggestion in VS Code editor for flutter. Every time when I declare a Widget in flutter then it shows me a suggestion to improve my flutter code that "Use 'const' with the constructor to improve performance" and It's keep coming. So I start searching for this error and found correct solution with explanation.

Explanation Why we have to Use 'const' with the constructor to improve performance:

Declaring widget with Const keywords will improve app performance by stopping rebuild of Widgets in flutter. Basic example is that when we update something in our app then the whole widget app tree will rebuild itself. It is not noticeable in small size apps but when you are developing a large size app with thousands of widgets inside it. It will improve performance because widgets defined with Const will not rebuild or re-render itself.

Error Description :

Use 'const' with the constructor to improve performance. Try adding the 'const' keyword to the constructor invocation.
Error Screenshot:
Flutter Use 'const' with the constructor to improve performance Error Fix

Flutter Use 'const' with the constructor to improve performance Error Fix:

1. To solve this error all we have to do is put const keyword before the Widget. See the below code example for more details.
  child: const Text(
            'Text',
            style: TextStyle(fontSize: 22, color: Colors.black),
            textAlign: TextAlign.center,
          )

Comments