What's the best programming practice to
create a constant class in Flutter
to keep all the application constants for easy reference. I know that there is const
keyword in Dart for creating constant fields, but is it okay to use static
along with const, or will it create memory issues during run-time.
class Constants {
static const String SUCCESS_MESSAGE=" You will be contacted by us very soon.";
}
My preferred solution is to make my own Dart library.
Make a new dart file named constants.dart
, and add the following code:
library constants;
const String SUCCESS_MESSAGE=" You will be contacted by us very soon.";
Then add the following import statement to the top of any dart file which needs access to the constants:
import 'constants.dart' as Constants;
You may need to run flutter pub get
for the file to be recognized.
And now you can easily access your constants with this syntax:
String a = Constants.SUCCESS_MESSAGE;