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:
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.
CodePudding user response:
See if this post can help you clarify:
https://stackoverflow.com/a/59114186/13658281
Const constructors inside build, for example, const Text('static text') will not cause this text to rebuild.
CodePudding user response:
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
.