10 Amazing Best Practices And Tips For Flutter Development

  Nikita Singh    May 25, 2021    467

 

Nearly 2.96 million apps on google play store and 2.2 million apps available on Apple’s app store. From these numbers, it is clear that mobile app development has become a need of the hour for all sizes of businesses of all domains. With the rapidly changing business sector and increasing competition, it has become difficult for all-level enterprises and startups to survive in the competitive market without having mobile applications for their business.

You can also know the reasons of- Why Flutter is setting the trend in mobile app development?

Flutter can make it more comfortable for new businesses to roll out with the feature rich mobile application without spending more money. And it will be more easy and effective if you know the best practices and tips of Flutter. If you’re thinking to develop a new flutter app for business, then this blog is just for you. Let us discuss the best practices and tips of flutter.

Best Practices And Tips For Flutter Development-

1. Naming convention-

Extensions name, classes, enums and typedefs should be in UpperCamelCase.

class MainScreen { … }
             enum MainItem { .. }
             Typedef Predicate<T> = bool Function(T value);
             Extension MyList<T> on List<T> { . . . }

Libraries, packages, directories and source files name should be in snake_case(lowercase_with_underscores).

library firebase_dynamic_links;
import 'socket/socket_manager.dart';

Variables, constants, parameters and name parameters must be in lowerCamelCase.

var item;
 Const bookPrice = 3.14;
Final urlScheme = RegExp('^([a-z]+):');
void sum(int bookPrice) {
// …
}

2. Specify types for class member-

Always specify the type of member when it’s value type is known. Avoid using var when possible.

//Don’t 
    Var item = 10;
    Final car = Car();
    Const timeOut = 2000;

//Do
Int item = 10;
Final Car bar = Car();
String name = ‘john’;
Const int timeOut = 20;

3. You can declare multiple variables with shortcut-

Generally, we declare variable most of the time this way-

int mark = 10;
int total = 10;
int amount = 10;

But if you are lazy, you can also do these things this way-

int mark =10, 
    total = 20, 
    amount = 30;

4. Load List Items On-Demand-

When working with infinite lists or extremely large lists, it’s better to use a ListView builder so as to improve performance. For this situation, you’ll need to provide a builder and the total number of items it should expect:

ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(‘${items[index]}’),
);},);

Also this is applicable when working with GridView that have a large or infinite number of widgets.

GridView.builder(
padding: const EdgeInsets.all(10.0),
itemCount: events.length,
itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: events[i],
child: EventItem(),
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1,
childAspectRatio: 3 / 2,
crossAxisSpacing: 10,
mainAxisSpacing: 10,),);

5. Split widgets-

In Flutter, your widget tree can turn out to be really enormous pretty quickly.  So to make your code readable and easily manageable, it is preferable to separate large widgets into separate files. Consider the below example where EventItem is stored in a separate file.

itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
value: events[i],
child: EventItem(),
),

6. Use Widget Builder-

While working in one file, individual widgets can also be huge. You can refactor that widget into a separate widget. In the long run, this will make your main widget leaner and more readable.

import ‘package:flutter/material.dart’;
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
 @override
Widget build(BuildContext context) {
return MaterialApp(
theme: 
ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
),
}
}
class MyWidget extends StatelessWidget {
Widget _sayHello(BuildContext context){
return Text('Hello, World!',style:
Theme.of(context).textTheme.display1 );
}
@override
Widget build(BuildContext context) {
return Container(
child: _sayHello(context),
);
}
}

7. Use if condition instead of conditional expression-

Most of the time you have to render a widget according to some condition in Row and Column. If conditional expression return null, then you should use if condition only.

//Don't
Widget getText(BuildContext context) {
return Row(
children: [
Text("Hello"),
Platform.isAndroid ? Text("Android") : null,
Platform.isAndroid ? Text("Android") : SizeBox(),
Platform.isAndroid ? Text("Android") : Container(),
]
);
}
//Do
Widget getText(BuildContext context) {
return Row(
children:
[
Text("Hello"),
if (Platform.isAndroid) Text("Android")
]
);
}

8. Use ?? and ?. operators-

Prefer the use of ?? (if null) and ?. (null aware) operators rather than null checks in conditional expressions.

//Don't
  v = a == null ? b : a;
//Do
v = a ?? b;
//Don't
v = a == null ? null : 
a.b;
//Do
v = a?.b;

9. Remove unused resources-

Particularly when you’re ready to publish your application, you’ll have to remove resources that you aren’t using in your application. These can be image assets. By removing unused resources and compressing images will reduce the app size.

10. Avoid print() calls-

debugPrint() and print() are used for logging in the console. If you use print()and output is too much at once, then sometimes Android discards some log lines. To avoid it use debugPrint().

Wrap up-

These tips gives you a clear insights to make your Flutter code more readable while also improving your app’s performance. If you’re facing any difficulty with Flutter app development or performance, consult with solace experts. We have a team of skilled flutter developers to help you through development and consultation. You can also hire flutter developers of solace team for effective and efficient development


 Article keywords:
flutter, apps, flutter development best practices

 


 Share this article: 
Print Digg StumbleUpon del.icio.us Facebook Yahoo! Buzz Twitter Google Bookmarks LinkedIn MySpace Orkut PDF Scoopeo Viadeo Add to favorites
      

© Copyright - Articles XP