By using "os library" as shown below
import os
if not os.path.exists('folder_name'):
os.makedirs('folder_name')

Ammar Javed
Still Learning | Still Searching | Still Exploring | The secret of your success is determined by your daily agenda.
Read More
Flutter is a mobile app SDK that allows you to create high-performance, cross-platform apps. Flutter includes a number of strong features that enable developers to create visually appealing and complicated user experiences. Flutter is highly extendable and comes with a variety of plugins that can be utilized during the development process.
For mobile applications, performance is critical. However, the number of missing frames and freezes affect the app's reliability, create a negative impression among customers, and eventually harm the organization. Overall, the performance of the Flutter program is satisfactory. Flutter is fast by default but we must avoid making several mistakes when writing the code in order for the application to work smoothly and quickly.
-----------------------
Related: The parameter can't have a value of 'null' because of its type in Dart
-----------------------
Contents
- Prefer Widgets over Functions
- Prefer Reducing App Size
- Prefer Const where Possible
- Avoid Rendering Frames
- Prefer nil over Widgets
- Prefer using Keys with Widgets
- Prefer itemExtent in ListView
- Prefer Dart Style
- Prefer RepaintBoundary Widget
- Prefer Widgets over Methods
- Prevent rebuilding all Widgets
- Prefer async/await Keywords
- Prefer String Interpolation
- Prefer Stateless Widgets
- Prefer Optimizing Animations
Prefer Widgets over Functions
There is no guarantee that you will have bugs by using functions, but by using classes you are guaranteed to avoid them. By using functions, you expose yourself to bugs and miss out on some performance optimizations.
Prefer Reducing App Size
During the development process, it is very simple to add a large number of packages to your code. Applying ProGuard rules to your Android app is one of the best techniques for reducing APK size. In XCode for iOS, go to the App Thinning section, select All compatible device versions, then Rebuild from Bitcode and Strip Swift symbols. The .ipa file should then be signed and exported.
Prefer Const where Possible
To minimize unwanted rebuilds caused by parent widgets, we must use const as much as possible for our widgets. Using a const allows us to save CPU cycles. When setState calls are made, the widget does not change; the const keyword prevents the widget from rebuilding.
Avoid Rendering Frames
Rendering at 60 frames per second on Android devices and 60 frames per second on iOS devices is recommended. This ensures that the application does not consume battery power or slow down the smartphone while running in the background.
Prefer nil over Widgets
When you wish to show nothing with little impact on performance, use nil package in the widget tree instead of a widget such as Container().
Prefer using Keys with Widgets
Prefer itemExtent in ListView
We have a list of 5,000 elements. When we hit the button, we will jump to the last part, but this will be a slow process because we are allowing the children to choose their own size. To prevent this, we must use the itemExtent parameter, which allows the scrolling mechanism to save effort by knowing the extent of the children. We receive an immediate jump with that tiny tweak, with no delays.
Prefer Dart Style
In UpperCamelCase, names capitalize the first letter of each word.
In lowerCamelCase, names capitalize the first letter of each word, with the exception of the first.
---------------
Related: Center Text in Container Flutter
---------------
Prefer RepaintBoundary Widget
If one of the RenderObjects in the same layer is designated as dirty, Flutter may repaint it. When a RenderObject has to be repainted, it notifies its nearest ancestor. The ancestor performs the same to the ancestor, maybe until the root RenderObject is reached. RepaintBoundary can detach ancestor render objects from descendant render objects, repainting just the subtree whose content changes.
Prefer Widgets over Methods
When we have a big design, we normally split it up using methods for each widget. Internally, when we make a change and refresh the full widget, it also refreshes the widgets that we have within the method, causing us to waste CPU cycles. We should transform those methods into a stateless Widget.
Prevent rebuilding all Widgets
Rebuilding StatefulWidget using setState, which rebuilds the entire widget again, but this is not a good practice; we only need to rebuild what we want to update. This can be accomplished using a state management package such as flutter bloc and provider, among others. However, it is also possible to do so without the use of an additional package, namely by utilizing the ValueNotifier and ChangeNotifier classes provided by the flutter framework.
Prefer async/await Keywords
If you wish to minimize development time, build strong code to avoid logical errors, and increase code readability, use null-check operators, and nullable operators.
Prefer String Interpolation
Using the operator + to do string operations instead, use string interpolation, which enhances the readability of your code and minimizes the likelihood of errors.
Prefer Stateless Widgets
To build our app, we use far too many stateful widgets. While stateful widgets require a lot of resources in order to rebuild the complete widget. Breaking down large widgets into several stateless widgets is one way to avoid heavy rebuild.
Prefer Optimizing Animations
Use fewer animations as much as possible. Instead of animating the user on the screen, for example, use a loading indicator to indicate that work is being done.
---------------
Related: Flutter use PNG as Icon
---------------
Flutter App Performance FAQS
1. Do Flutter apps faster?
Flutter apps are fast and customizable.
2. Do Flutter have good performance?
With Flutter, we can write high-quality apps with high performance.
3. Is Flutter faster than React or not?
Flutter is much faster than React Native.
Why does this Error Occur?
This error occurs because non-nullable parameters cannot be null with null safety enabled.
Values may be null in the function and the constructor when the function is called without the named parameter. This is incorrect because the types are non-nullable and must never be null.
How to Solve this Error?
required
This means that a variable is essential.
void sum({required int number}) {
// ...
}
The number parameter must be given a value at all times.
The default value
If a parameter contains a default value, we can call the function without specifying the parameter value because the default value will be used instead:
void sum({int number = 10}) {
// ...
}
Now, when we will call sum(), the number parameter will have a value of 10, which is definitely non-null.
Nullable argument
We'll need to null-check the argument before utilizing it in your function.
However, because we don't always want to supply a key to the widget in Flutter (nullable Key? type), this is the most frequent approach to handle the Key key issue:
class Fuu extends StatelessWidget { const Fuu({Key? key}): super(key: key); // ... }
Positional Parameter
Positional arguments can be declared nullable or non-nullable. However, they cannot be marked with required or have default values because they must always be passed.
void fuu(int p1) {} //is invalid. void cuu(int? p1) {} //is valid.
can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Why does this Error Occur?
This error occurs because non-nullable parameters cannot be null with null safety enabled.
Values may be null in the function and the constructor when the function is called without the named parameter. This is incorrect because the types are non-nullable and must never be null.
How to Solve this Error?
required
This means that a variable is essential.
void sum({required int number}) {
// ...
}
The number parameter must be given a value at all times.
The default value
If a parameter contains a default value, we can call the function without specifying the parameter value because the default value will be used instead:
void sum({int number = 10}) {
// ...
}
Now, when we will call sum(), the number parameter will have a value of 10, which is definitely non-null.
Nullable argument
We'll need to null-check the argument before utilizing it in your function.
However, because we don't always want to supply a key to the widget in Flutter (nullable Key? type), this is the most frequent approach to handle the Key key issue:
class Fuu extends StatelessWidget { const Fuu({Key? key}): super(key: key); // ... }
Positional Parameter
Positional arguments can be declared nullable or non-nullable. However, they cannot be marked with required or have default values because they must always be passed.
void fuu(int p1) {} //is invalid. void cuu(int? p1) {} //is valid.
You might be interested in
-
the parameter cannot have a value of null flutter
06 Jun, 2022 1,171 views -
can't have a value of 'null' because of its type, but the implicit default value is 'null'.
06 Jun, 2022 1,377 views