Dart 'const' Tutorial – All You Need to Know (Const Expressions, Constructors, Canonical Instances)

preview_player
Показать описание
📗 Learn from the written tutorial 👇👇

📧 Get Flutter news 📰 and resources:

👨‍💻 Do you write good code? Find out now!

Constants are not just an obnoxious version of final variables that will haunt you in your sleep with all of the errors associated with them. Compile-time constants are actually a nice way to improve the performance of your app by not instantiating the same object multiple times and additionally, so to say, "pre-instantiating" objects at compile time.

Go to my website for more information, code examples, and articles:

Follow me on social media:
Рекомендации по теме
Комментарии
Автор

Another benefit of constants is the compiler can use them together with if blocks to determine if entire blocks of code even need to be compiled at all. For example, Flutter has the "kDebugMode" bool constant. If you have some code "if (kDebugMode) { ... }" and the app is being compiled in release mode, the value of "kDebugMode" will be false. The compiler will see this and will omit all the code in "..." from the compiled app. You can use this to clear out entire sections of code to keep the compiled size of your app down by eliminating entire sections of code that the production version won't even need.

Abion
Автор

This is just what I needed, I wasn't aware of const being used for constants like edgeinsets.. But it makes sense now

samuelcatlow
Автор

Thanks for making it clear about the benefits of using the const keyword

lucyanxyz
Автор

Very useful, Matt. This video came just on time and helped me a lot. Keep up the great work.

Hossam
Автор

So should we const every single Widgets possible or just the parent widget?


For example:
const Center (child: const Text (' hello with const', style: const Textstyle( ... )))


or


const Center (child: Text (' hello without const'), style: Textstyle( ... ))

alvin
Автор

It would be nice to see a more realworld use case using Stacks, Columns, Containers, based on incoming data. I always struggle with keeping anything const irl. It seems like your data must be fully immutable, and then you can kinda work with it, but even then it seems to always turn into a big headache that you must always be thinking out.

ShawnBlais
Автор

What a brilliant tutorial! Do you offer courses on Udemy?

dhoomketu
Автор

I've noticed you are very good with the hotkeys haha

gerooq
Автор

Why we can not pass final variables to const constructor ?? Is it mandatory to use 'const' keyword while instantiating a const constructor ?? What happens if i don't use 'const' keyword for instantiating a const class ??

pradeepkumarreddykondreddy
Автор

Thanks for this vid mate it was super helpful.

Can anyone tell me what vscode theme that is?

jobbies
Автор

Hey Matt, slightly unrelated question but does explicitly mentioning the static type benefit performance, as the compiler has less guess work?

gerooq
Автор

Why would one need to define a constant constructor when the aim is to create a new instance? in other words, what's the use case of a const constructor other than a canonical instance? Thanks a lot!

barsidemen
Автор

what font did you use in this tutorial? Fira Code? btw thanks for the awesome tutorial. :)

dhanushkakumarage
Автор

What if my class has static consts and final fields should I still make the constructor const?

xaxababa
Автор

I am looking for a video series on MVVM design pattern using Providers like that you one have for android

adityajoshi
Автор

What about `static`? I hope you clear the ambiguity there as well in a follow up for this video:)

aytunch
Автор

"Future is not Constant! " Looks like a line straight from a scifi time travel movie.

tashijawed
Автор

Maybe change the title to Dart const... (lowercased)

chordfunc
Автор

I really like your videos but you make a lot of assumptions here. Using const could be pre-emptive optimisation at the expense of readability. As far as I've seen, the performance gains are almost negligible.

cedvdb
Автор

this is flat out wrong, I have tested the use of CONST extensively and it provides absolutely no performance increase at all. I even tested it in specific situations where it is claimed to have the biggest speed increase and there was none whatsoever. Not sure where this CONST bandwagon came from, but developers need to actually test this stuff on their own. Show me some real proof.

rockstylex