Hi Guy’s Welcome to Proto Coders Point. In this Flutter Article let’s learn about Naming Convention in Flutter Application development. As you all know that in the world of software development, Giving a Preper naming conventions plays a crucial role in maintaining a clean, understandable, and easily maintainable code. As Flutter, is Developed by Google’s it is a UI toolkit for building natively compiled applications that supports on all the platform may be it’s mobile, web, and desktop that too with a single codebase. By maintaining a proper naming conventions while developing Application in Flutter will not only enhance readability but also make it easier for developers to collaborate on projects. This article will take you to the best practice that you need to follow while using naming convention in flutter.
1. Files and Directories
Files:
- Use lowercase_with_underscores for naming file names. This kind of format is way easier to read and avoids potential issues with case sensitivity across different operating systems.
- Example:
main.dart
,home_screen.dart
,user_profile.dart
Directories:
- Similarly, while creating directories always use this naming convension “lowercase_with_underscores”
- Example:
assets
,models
,services
,widgets
2. Classes
Then comes Classes in dart language. While creating classes names should be written in Pascal Case, where first letter of word starts with an uppercase letter and there sholuld be no spaces or underscores. This kind of naming convention format is mostly common in many programming languages like Java, JavaScript, Python.
- Example:
HomePage
,UserProfile
,ApiService
3. Methods and Functions
In Dart Methods and functions should be written in camelCase, Means here Method/ Function will start with first letter as lowercase, and can be give uppercase letter for subsequent word.
- Example:
fetchData()
,getUserProfile()
,handleButtonClick()
4. Variables
Instance variables and local variables:
- For declaring variables in dart we make use of camelCase, starting with a lowercase letter and capitalizing subsequent words.
- Example:
userName
,profilePicture
,userAge
Constant variables:
- For declaring constant variable what will never changed, Make use of Full uppercase letters and can use underscores separating words for better understanding the use case.
- Example:
MAX_USER_AGE
,DEFAULT_PROFILE_PICTURE
5. Constants and Enums
Constants:
- As mentioned above, constants should always be names with all uppercase letters with underscores if needed.
- Example:
const int MAX_RETRIES = 3;
Enums:
- Enum names should be in PascalCase, and their values should be in camelCase.
- Example:
dart enum UserStatus { active, inactive, banned }
6. Widgets
StatelessWidget and StatefulWidget Classes:
- For State Widget naming convension should be PascalCase convention widget classes.
- Example:
LoginButton
,UserCard
State Classes:
- When defining the state class for a
StatefulWidget
, This class name should be as Normal at said above and by appendingState
at the end of class name to make it as widget class name. - Example:
class LoginButton extends StatefulWidget { @override _LoginButtonState createState() => _LoginButtonState(); } c lass _LoginButtonState extends State<LoginButton> { // State implementation }
7. Private Members
Private Memebers in dart are always defined with _
at begin of the naming convension, This can be applied to variable, method or class to make it private member.
- Example:
_fetchData()
,_userName
,_UserService
8. Mixins
Mixin Variable are maded as PascalCase, At the end of theWord or suffix word Mixin for easily identify the Mixin Variable in dart.
- Example:
LoggingMixin
,ValidationMixin