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.

constats.dart

const String BUTTON_LABEL = ‘Submit’;

The const keyword i…


This content originally appeared on DEV Community and was authored by Rui.Z

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';

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),
)
...


This content originally appeared on DEV Community and was authored by Rui.Z


Print Share Comment Cite Upload Translate Updates
APA

Rui.Z | Sciencx (2021-06-04T20:20:54+00:00) How to keep all constants in Flutter?. Retrieved from https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/

MLA
" » How to keep all constants in Flutter?." Rui.Z | Sciencx - Friday June 4, 2021, https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/
HARVARD
Rui.Z | Sciencx Friday June 4, 2021 » How to keep all constants in Flutter?., viewed ,<https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/>
VANCOUVER
Rui.Z | Sciencx - » How to keep all constants in Flutter?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/
CHICAGO
" » How to keep all constants in Flutter?." Rui.Z | Sciencx - Accessed . https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/
IEEE
" » How to keep all constants in Flutter?." Rui.Z | Sciencx [Online]. Available: https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/. [Accessed: ]
rf:citation
» How to keep all constants in Flutter? | Rui.Z | Sciencx | https://www.scien.cx/2021/06/04/how-to-keep-all-constants-in-flutter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.