I have in my mind that dart will use the const
constructor if it is able to do it automatically, to explain the hypothesis lets assume that we have a widget that already have a const constructor
like:
class Retry extends StatelessWidget {
const Retry();
}
And then because dart "is able to infer the const ussage" both of the next codes will mean and be compiled into the same code:
1.
Container(
child: Retry()
)
Container(
child: const Retry()
)
Is this assumption that dart can infer that he must use the const
constructor for a class that have that option declared? Or is not?
How can I corroborate it?
Dart cannot infer that you want that object to be const
unless there is some context surrounding it that forces it to be const
. This is not the case in the example you show. You must use the const
keyword at some point if you want dart to make something const
.
Example of inferred const
:
const SizedBox(
child: Retry(),
)
The const
on the SizedBox
forces Retry
to be const
. This and similar situations are the only places where const
is implied.