Flutter: Defining Constants — The Right Way
Update: With the release of Dart 2.17, it’s possible to carry data in enums. Most contents in this article are now outdated; however, you can still use them as a reference for compatibility with older Dart SDK versions.
For more info, check out the updated example or the following article:
In programming we often define constants. If you don’t know the benefits of using constants, check this StackOverflow question. While there are different ways to define constants in Flutter, let’s talk about them.
Assume I’m writing a mobile application, it stores user preferences in local storage. Each value in the local storage must have a key (e.g. save_user_id: true
, save_user_id
is the key and true
is the value), I’m going to define the keys in constant.
1. Plain