Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best practice to keep all the constants in Flutter?

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."; } 
like image 459
Manoj Perumarath Avatar asked Jan 07 '19 06:01

Manoj Perumarath


People also ask

How do you use const in Flutter?

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 do you avoid const in Flutter?

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.

What is static const in Flutter?

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.


2 Answers

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:

enter image description here

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; 
like image 181
Anirudh Avatar answered Sep 18 '22 04:09

Anirudh


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:

  • Works with hot-reload
  • Easily testable and mockable
  • Can be replaced with something more dynamic than constants without rewriting the whole app.

But at the same time it:

  1. Doesn't consume much more memory (the inherited widget is typically created once)
  2. Is performant (Obtaining an InheritedWidget is O(1))
like image 27
Rémi Rousselet Avatar answered Sep 17 '22 04:09

Rémi Rousselet