Best Coding Practices And Tips In Flutter

Best Coding Practices And Tips In Flutter :

  • Any programmers need to improve code quality, readability maintainability, and robustness so in the future when we have to work on that app we can easily find specific code.
  • Let’s see some best coding practices for designing and developing application in flutter.
  1. Declaration (lowerCamelCase): Give name of variables, constants, parameters and named parameters in lowerCamelCase.
String name;
intphoneNumber = 123456789;
constsongPrice = 9.99;

void sum(inttotalPrice){

2.Variable initialize (don’t null):

Don’t explicitly initialize variables to null.

Dart by default variable automatically initialized null when their values are not specified.

String name = null; 

String name;

void error([String? message]) {}

void error([String? message = null]) { }

3.Naming convention (Uppercase): Give the name of any Class, extension, enums in

classLoginScreen{ }

extensionGenericList<T> on List<T>{ }

enumOrderStatus{ }

4.Packages, Libraries, source files should be in snake_case.

import 'package:font_awesome_flutter/font_awesome_flutter.dart';

5. import prefixes using lowercase_with_underscores.

import 'dart:math' as Math;
import 'package:angular_components/angular_components' as angularComponents;
import 'package:js/js.dart' as JS;

import 'dart:math' as math;
import 'package:angular_components/angular_components' as angular_components;
import 'package:js/js.dart' as js;

6. package imports (relative import):

Your application lib file don’t show import package your application package name.

When we using relative imports together then it’s possible to create confusion when the same class gets imported from two different ways.

To avoid that case we should have to use a relative path in the /lib folder.

When package import cursor move to import you show 5 different options:

  • Add explicit ‘show’ comninatior
  • Convert to a relative import
  • Convert to double quoted string
  • Convert to multiline string
  • Inject language or reference
// Don't
import 'test/test_file/test_images/images_id.dart';

// Do
import '../test_images/images_id.dart';

7. Use ??and ?. operators

In dart to check nullability of variables use ?? (if null) and ?. (null aware) operators instead of checks null in a conditional expression.

// Do
if (optionalThing?.isEnabled ??false) { }

// Do
if (optionalThing?.isEnabled ??true) { }

// Don't
if (optionalThing?.isEnabled == true) {
print("Have enabled thing.");

// Don't
a = x == null ?y : x;

// Do
a = x ??y;

// Don't
a = x == null ?null :x.y;

// Do
a = x?.y;

8.Use Cascades Operator

If we want to do a sequence of operations on the same object then we should use the Cascades(..) operator.

// Don't
var size = Size();
size.lineTO(0, size.width);
size.lineTo(size.height, size.width);
size.lineTO(size.width, 0);

// Do
var size = Size()
..lineTO(0, size.width)
..lineTO(size.height, size.width)
..lineTo(size.width, 0)

otherChargeList: [
OtherCharge(name: 'GST', chargeAmount: 200),
OtherCharge(name: 'Delivery charge', chargeAmount: 100),
OtherCharge(name: 'Discount', chargeAmount: 100),
OtherCharge(name: 'CGST', chargeAmount: 50),
OtherCharge(name: 'SGST', chargeAmount: 50)

9. Use debugPrint() for logging in to the console.

For logging in to the console if you use print() and output is in too many lines, then Android sometimes discards some logs line. To avoid this use debugPrint().

10. Use const in widgets

When setState call all widgets will rebuild so it’s a decrease performance. so define const to those widgets which will not change when setState call.

constTextStyle(fontSize: 20)

11.Reusability of code

When some code is used multiple times without any changes at that time don’t write the same code multiple times .make one widget for that and use that widget when it needs.

For example: TextField, Button as depends on design.

12.Bulk declaration

If you declaring each variable separately, you can declare a variable of the same datatype at once.

// Don't
class Student{
  String name = "Lakhani";
String lastName = "Kamlesh";
double height;
double weight;

// Do
class Student{
String name = "andres", lastName = "Messi";
double height, weight;

13.Use GoogleFonts

When you have to use google fonts in a flutter, don’t need to download that font. you can directly use google font using GoogleFonts.FontName().

Text( "Sample Text", style: GoogleFonts.fraunces(textStyle:TextStyle(color:

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit