Coding Style
The important part of good code is good style
Search by using Rex
Useful
// not recommend
var result = double.parse('some string');
//1. This code could be have error or crash app
// when string value not able to case into doubleimport 'package:infinity_core/core.dart';
var result = 'some string'.secureDouble;
// 1. use existing build in function,
// if some string have invalid format it function will take care for us.if(true){ return; } if(false){ return; }// not recommend AppBar( backgroundColor: Theme.of(this).colorScheme.inversePrimary, title: const Text('API URL', textAlign: TextAlign.center), ); ElevatedButton( onPressed: _settingOnPressed, style: ButtonStyle(backgroundColor: WidgetStateProperty.all<Color>(Theme.of(this).colorScheme.inversePrimary)), child: Text(_isEditApi ? 'Done' : 'Change API'), ); // This code have use inversePrimary which is primary color, and this color base on context and use multiple place. // do not copy and write redundancy // recommend extension BuildContextExtension on BuildContext { Color get colorPrimary => context.colorPrimary; }; AppBar( backgroundColor: context.colorPrimary, title: const Text('API URL', textAlign: TextAlign.center), ); ElevatedButton( onPressed: _settingOnPressed, style: ButtonStyle(backgroundColor: WidgetStateProperty.all<Color>(context.colorPrimary)), child: Text(_isEditApi ? 'Done' : 'Change API'), ); // To resolve about case we can create other extension for context. // next time we only need to write context.colorPrimary.class MyClass { String _privateVariable = "This is private"; String publicVariable = "This is public"; void _privateMethod() { print("This is a private method."); } // Public method (accessible from other files) void publicMethod() { print("This is a public method."); // Calling the private method from within the same file _privateMethod(); } } void main() { MyClass myClass = MyClass(); print(myClass.publicVariable); myClass.publicMethod(); // Accessing the private variable and method will result in a compile-time error // print(myClass._privateVariable); // Error: '_privateVariable' isn't defined // myClass._privateMethod(); // Error: '_privateMethod' isn't defined }void main() { var myClass = MyClass(); // Using 'var' instead of repeating type print(myClass.publicVariable); myClass.publicMethod(); }class UserInfo { var _status = "Active"; var userName = "John Doe"; void showInfo() { print(userName); // Accessing public variable _logStatus(); // Call to private method } } void main() { var user = UserInfo(); // Using var and meaningful name print(user.userName); user.showInfo(); }class UserInfo { String _status = "Active"; String userName = "John Doe"; // Public method to set user status with strict type void setStatus(String newStatus) { _status = newStatus; } } void main() { var user = UserInfo(); user.setStatus("Inactive"); }class UserInfo { // Properties with no default values declared directly String userName; String _status; // Constructor with default values UserInfo({String? userName, String? status}) : userName = userName ?? "John Doe", // Default value assigned here _status = status ?? "Active"; }void main() { var user = UserInfo(); // Not recommend if(user.userName != ''){ } // Recommend print(user.userName.isEmpty()); // True }void main() { // Creating a UserInfo without specifying the userName and status var user = UserInfo(); // Not recommend print(user.userName.toString()); // Recommend print(user.userName); // Its already string type as define in Model }
Identifiers
DO name types using UpperCamelCase
UpperCamelCaseDO name extensions using UpperCamelCase
UpperCamelCaseDO name packages, directories, and source files using lowercase_with_underscores
lowercase_with_underscoresDO name import prefixes using lowercase_with_underscores
lowercase_with_underscoresDO name other identifiers using lowerCamelCase
lowerCamelCasePREFER using lowerCamelCase for constant names
lowerCamelCase for constant namesLast updated