Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I remove const in the below flutter code?

After updating to flutter 2.5 the const modifier is added to the default code and there are places where const is to be added where we don't have to earlier. so what's the difference between adding a const?

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}
like image 683
Riddhipratim Adhikari Avatar asked Oct 12 '25 09:10

Riddhipratim Adhikari


1 Answers

It is a good practice using the const keyword on any data type or widget that uses static data.

However if you fill like removing it, you can edit your analysis_options.yaml file and add the following:

open analysis_option.yaml file & under linter > rules disable 



> prefer_const_constructor.
>     
>         linter:
>           rules:
>             prefer_const_constructors: false

Then, after making prefer const constructors: false, you need to run a command in IDE terminal as below:

'dart fix'

if you see log like that => **

"This tool looks for and fixes analysis issues that have associated automated fixes."

** then try this

'dart fix --dry-run'

'dart fix --apply'
like image 135
Hamza Muazzam Avatar answered Oct 15 '25 00:10

Hamza Muazzam