Five Tips for Your Next Flutter Project
As more and more companies move to cross-platform development, Flutter inevitably becomes the most preferred cross-platform technology. Most updates to Flutter include improved platform compatibility, contributing to its growth.
Flutter is an open-source UI development kit created by Google. It is used to develop cross-platform applications for Android, iOS, Linux, macOS, Windows, Google Fuchsia, and the Web from a single code base.If you’re new to Flutter, follow the instructions at this link to complete the installation and get started with Flutter.
If you’re starting with Flutter or are building applications on Flutter, these tips will help you at some point, perhaps on your next project. Feel free to share your tips in the comments.
1. Create Applications that Rely Less on Packages
Don’t rely heavily on packages, as they can become obsolete, leaving you with extra work to create fixes.Although it depends on the complexity of the feature, some packages, such as adding animations to containers, can be left out because it may be something you can write from scratch. This will give you more control over the widget, allowing you to change it easily.Update the pubspec.yaml file regularly, and remove packages that were added earlier but are no longer used in the project.
2. Setting up recurring widgets
Widgets that are constantly repeated in the project can be separated into a separate devices and used in different places. This avoids writing the same widget repeatedly and saves time and effort.
Let’s look at an example of a screen with four buttons. Instead of writing the button code with onPress, color, and other properties four times, I will extract the button into a separate widget and then pass those properties to a variable.The CommonButton widget is called four times, and each time it is called it gets the onPress, color, and text properties. Let’s now look at the code of the CommonButton widget.
The variables will be different each time we call the CommonButton, and that’s how we can reuse the CommonButton widget multiple times in the project.
3. Use log() instead of print()
Flutter 2.5 and up comes with the flutter_lints package, which identifies possible problems in the dart source code.Every time you use print(), it warns: “Avoid ‘print’ calls in production code”. The package is a warning because print logs in release builds can record sensitive information. To avoid this, use log(), a built-in dart function that helps find bugs in code and checks for performance issues, coding styles, and formatting.
4. Stateless or Hooks over Stateful widget
Stateful widgets put more strain on the application than Stateless and Hook widgets because they are involved in rebuilding the device.Avoid using Stateful widgets as much as possible. If you are new to Flutter Hooks, I wrote an article explaining how to use Hook widgets instead of Stateful widgets.
5. Common File for Constants
A standard file to house all constants, such as colors, asset strings, embellishments, and text styles, can make the development process go smoothly.
Here’s an example of a constant file,Now you don’t have to define a constant every time throughout your program. Define it once in the constants. dart file and use it in different places. You can reuse the constant file for multiple projects.
Do you have any advice you want to share? Feel free to leave them in the comments and share if they were helpful to you!