What\'s the best programming practice to
create a constant class in Flutter
to keep all the application constants for easy referenc
That's completely up to you.
Using static has no disadvantages.
Actually const
is required for fields in a class.
My preferred solution is to make my own Dart library.
Make a new dart file named constants.dart
, and add the following code:
const String SUCCESS_MESSAGE=" You will be contacted by us very soon.";
Edit: 99% of the time you don't need to explicitly name your dart libraries with a statement like library library_name;
at the top of your file, and you probably shouldn't (reference).
Even if you leave out this line your file will still be library! It will just be implicitly named.
Then add the following import statement to the top of any dart file which needs access to the constants:
import 'constants.dart' as Constants;
Note if constants.dart
is in different directory then you will need to specify the path to constants.dart
in your import statement.
In this example:
You could use a relative path:
import '../assets/constants.dart' as Constants;
Or an absolute path from the lib directory:
import 'package:<your_app_name>/assets/constants.dart' as Constants;
Now you can easily access your constants with this syntax:
String a = Constants.SUCCESS_MESSAGE;
I've noticed a little confusion across the answers here, so I thought I would try to clear a few things up.
Dart/Flutter guidelines suggest not creating classes that only contain static members, namely because it isn't necessary. Some languages, such as Java or C# will not allow you to define functions, variables, or constants outside of a class, but Dart will. Therefore, you can simply create a file such as constants.dart that contains the constants that you want to define.
As noted by @Abhishek Jain, the library
keyword is not required for this method to work. The library
keyword is used for libraries that will be published for use in other projects, although it can be used along with part
and part of
to break a single library up across multiple files. However, if you need that, then your needs are probably beyond the scope of OP's question.
As pointed out by @ChinLoong, it is technically acceptable to create a class that groups related constants and enum-like types. It should be noted however that this demonstrates an exception to the guideline as it is not a hard rule. While this is possible, in Dart, it is frowned upon to define a class that is never instantiated. You'll notice that the Dart Color class that defines Color constants has a constructor which excepts an integer value, allowing instantiation for colors not defined by a constant.
In conclusion, the approach that best adheres to the Dart guidelines is to create a constants.dart file or a constants folder containing multiple files for different constants (strings.dart, styles.dart, etc.). Within the constants.dart file, define your constants at the top level.
// constants.dart
const String SUCCESS_MESSAGE=" You will be contacted by us very soon.";
...
Then, import
the file wherever the constants need to be used and access directly via the constant's name.
Dart documentation says explicitly "AVOID defining a class that contains only static members." dart.dev/guides/language/effective-dart/design
The approach I use is creating one or more files to store those constants. Sometimes, when your project is too big it might have a lot of them, and in order to load less data, I prefer to separate them by the different contexts I would use them creating separate files. So I only import the ones that I would use.
For all constants, just create constants.dart file under lib folder or lib/util folder, then keep all constant variables as follows :
const SUCCESS_MESSAGE=" You will be contacted by us very soon.";
// Api related
const apiBaseURL = "https://baseurl.com";
const userLoginApi = "login";
const userSignupApi = "signup";
// Shared Preference keys
const kDeviceName = "device_name";
const kDeviceUDID = "device_id";
// Asset Constants
const navBarLogoImage = "assets/images/home_images/sample.png
then import constants.dart file in required class and use it directly.
I like to organise my "constants" this way. It's easier for me to stay organised and to keep track what's already there.
I can than do stuff like this: Tables.questions.id
and Tables.anwerOptions.orderIndex
class Tables {
static QuestionsTable get questions => QuestionsTable();
static AnswerOptionsTable get answerOptions => AnswerOptionsTable();
}
class QuestionsTable {
String get id => "id";
String get title => "question";
String get subtitle => "description";
String get inputFieldType => "answer_input_type";
String get answer => "answer";
}
class AnswerOptionsTable {
String get id => "id";
String get questionId => "question_id";
String get answerOption => "answer_option";
String get orderIndex => "order_index";
}