DEV Community

Rui.Z
Rui.Z

Posted on • Updated on

 

How to keep all constants in Flutter

A clean and simple way to keep constants in Flutter is to make an own Dart library in the project for the constants.

For example, we have a project structure like this.
image

constats.dart

const String BUTTON_LABEL = 'Submit';
Enter fullscreen mode Exit fullscreen mode

The const keyword is used to represent a compile-time constant. Variables declared with const keyword are implicitly final. Link

You can then use import statement to access to the constants:

import 'package:<project_name>/assets/constants.dart' as constants;
...
ElevatedButton(
  child: const Text(constants.BUTTON_LABEL),
)
...
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
khalilpan profile image
khalil panahi

Nice to know 👏👏

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout 😆