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."; }
The const keyword is used when the value of the variable is known at compile-time and never changes. In other words, the compiler knows in advance what value is to be stored in that variable. const int x = 1;//At compile time, the value of x is going to be 1 and will not change.
How to Ignore 'prefer const constructors' message in the current file only: To ignore this message in a specific file only, add the following line anywhere in your dart script file. We have added this line after importing, you can add anywhere you want in the file where you want to ignore 'const' error warning.
You have to declare it as static const rather than just const . static , final , and const mean entirely distinct things in Dart: static means a member is available on the class itself instead of on instances of the class. That's all it means, and it isn't used for anything else. static modifies members.
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.
EDIT: Use lowercase_with_underscores
when specifying a library prefix.
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;
EDIT
Now that the flag --dart-define
has been added to the different command lines of Flutter, the following answer no-longer applies.
Instead just declare constants wherever you want, and potentially refer to other answers.
While there are no technical issues with static const
, architecturally you may want to do it differently.
Flutter tend to not have any global/static variables and use an InheritedWidget.
Which means you can write:
class MyConstants extends InheritedWidget { static MyConstants of(BuildContext context) => context. dependOnInheritedWidgetOfExactType<MyConstants>(); const MyConstants({Widget child, Key key}): super(key: key, child: child); final String successMessage = 'Some message'; @override bool updateShouldNotify(MyConstants oldWidget) => false; }
Then inserted at the root of your app:
void main() { runApp( MyConstants( child: MyApp(), ), ); }
And used as such:
@override Widget build(BuilContext context) { return Text(MyConstants.of(context).successMessage); }
This has a bit more code than the static const
, but offer many advantages:
But at the same time it:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With