Flutter: Best Practices — 1
Today I’ll write about one of the best practices in Flutter. We know these little tips, but we sometimes ignore them when we trying to complete the project before the deadline. We only need to fix them when they cause a real problem. In this case, retrospective code editing work causes us extra time loss.
Coming to our topic, I thought it didn’t matter how we used it when calling custom widgets we wrote. I wrote a custom drawer menu which is a method. When I call that method, Flutter rebuilds it unnecessarily. You can see that from debug console.
Moreover, Flutter rebuilds that widget even onPressed of FloatingActionButton. I don’t need to rebuild it. Those unnecessary rebuildings will cause a real CPU recycling and sometimes it gets annoying.
So, I changed my method to a const Widget by defining like const CustomDrawer()
. Now it is called only when pressing to drawer widget.
I was inspired and got the answer from here:
https://iiro.dev/splitting-widgets-to-methods-performance-antipattern/
You can also read this article about the best practices and tips in Flutter:
https://medium.com/flutter-community/flutter-best-practices-and-tips-7c2782c9ebb5
Here is my GitHub repo about this article: