Article

Flutter Developer Guide, Part IV: Avoid These Common Mistakes

Slide 16_9 – 5

Our previous posts (Part 1, Part 2, and Part 3) covered various use cases of tips and tricks to enhance readability, performance, and maintainability in development.

This post takes a different direction by highlighting aspects to avoid during development, offering insights to save you from potential headaches and elevate your expertise.

These upcoming tips aim to streamline your code, boost readability, and help you sidestep inefficiencies and common mistakes. By steering clear of these issues, you can make your code more concise and robust, ultimately becoming a more effective and knowledgeable developer.

Avoid using “Border.all” constructor

It is advisable to refrain from using the ‘Border.all’ constructor in Flutter, especially in scenarios involving multiple widgets with borders or animations, as it can potentially lead to performance issues. This is attributed to the underlying workings of Flutter’s rendering engine.

When ‘Border.all’ is utilized, a new ‘Border’ object is generated each time the widget is rebuilt. Given the immutability of the ‘Border’ object, this practice creates numerous unnecessary instances, consuming memory and potentially triggering unwarranted widget rebuilds.

Flutter offers a more efficient alternative through the Border class to mitigate this concern. Creating and defining border properties once and reusing them can significantly enhance performance and reduce memory consumption. Opting for this approach ensures a more streamlined utilization of borders within your Flutter application.

Avoid using ‘as’—instead, use ‘is’ operator

Typically, the ‘as’ cast operator in programming throws an exception if the cast is not feasible. To sidestep the possibility of an exception, developers can opt for the ‘is’ operator to verify the cast before proceeding.

Employing the ‘is’ operator before using the ‘as’ cast operator is beneficial for preemptively checking the feasibility of a type conversion in programming. By first confirming whether the cast is possible with ‘is’, developers can avoid runtime exceptions that may occur when using ‘as’ alone. This approach enhances error handling, allowing for more graceful responses when a cast cannot be executed successfully. It contributes to the overall robustness of the code by preventing crashes and ensuring controlled behavior in scenarios where the type conversion may face challenges. Additionally, incorporating ‘is’ for type checking enhances code readability, clarifying intentions and facilitating debugging and maintenance processes. This defensive programming practice adds a layer of reliability to the code, promoting a smoother execution and reducing the risk of unexpected runtime errors.

Avoid using leading underscore for local identifiers that aren’t private

In Dart, a leading underscore (_) marks members and top-level declarations as private. It’s important to note that this convention does not extend to local variables, parameters, local functions, or library prefixes. Therefore, avoid unnecessary underscores in the names of these local entities to adhere to Dart’s naming conventions and maintain code clarity.

Avoid ‘print()’ calls

In Android, both `print()` and `debugPrint()` serve as console logging functions. However, when utilizing `print()`, there is a risk of losing log lines if there is an excess of output. To mitigate this, opting for `debugPrint()` is advisable, which helps prevent the discarding of log lines in situations with extensive output. For cases where logging demands a higher level of detail, consider leveraging `dart:developer log()`. This alternative provides increased granularity and richer information in the logging output, enhancing the overall effectiveness of debugging and analysis.

Avoid explicitly initializing variables ‘null’

In Dart, when a variable’s value is not explicitly specified, it is automatically initialized to null. Therefore, explicitly adding null is redundant and unnecessary in such cases.

Avoid creating a lambda when a tear-off will do

If a function simply invokes a method with the same arguments it receives, there’s no need for manual wrapping of the call in a lambda expression. The function can directly delegate the arguments, simplifying the code and improving readability.

The key to improving as a Flutter developer? Fine tune the basics.

Steering clear of these common pitfalls enhances your application’s performance, particularly in scenarios involving animations or resource-intensive widget applications. Meticulous attention to these finer details and implementing subtle enhancements can significantly impact overall efficiency.

In our discussion, we highlighted the importance of avoiding the “Border.all” constructor, utilizing ‘is’ instead of ‘as’, refraining from using leading underscores for non-private local identifiers, reconsidering print calls, eschewing explicit null initializations, and judiciously choosing between creating a lambda and using a tear-off.

Stay tuned for our next post, where we’ll delve deeper into additional tips, focusing on widgets to enhance their usability further while maintaining the high coding standards and performance expectations.

Ricardo Colin Picture

Ricardo Colin

Sr. Mobile Developer

Ricardo comes to Fresh with experience in creating websites and custom applications for smaller companies from when he was in college, with a focus on mobile development. After developing Android applications on Java, he transitioned to iOS using Swift. Then, once Flutter developed a cross-platform technology, Ricardo became intrigued and began using that framework for future applications.

Ricardo also enjoys guiding and helping other devs and peers with projects, previously acting as a part-time coach to students with Flutter and Firebase projects. In addition, Ricardo has two Flutter game applications in the App Store and the Play Store: “Math Circle Roulette,” which helps with practicing math problems, and “What’s My Number?” a guessing game that you can play with other friends or with a robot.

Ricardo loves to learn and talk about almost anything, especially about new technology and finding new opportunities to exchange knowledge. Outside of work, he loves to learn about new places to hike and will play soccer from time to time.