Expert Build Variants For Flutter Apps
The Dart Compiler has some new settings that allow us to define compile-time DEFINE variables and define a target main file, but it takes a strategy to use them correctly. I am going to show you a strategy that keeps you from having to duplicate code into separate mains in each build variant.
The reason why we use build variants is to be able to change environment parameters per our debug, testing, and staging phases of app development. Hence, it’s part of DevOps.
Build Modes
We have three build modes:
- Debug
- Profile
- Release
Profile mode is actually a debug mode with some debugging being limited. We actually want to map this to:
- dev
- staging
- prod
Since we can delegate by having a function return another function when it executes, we have a way to avoid having to put the main function in each build variant. The way it looks in UML only shows the constants environment part:
Constants Dart File
Here is our constants dart file:
We use a _Config private class to limit access, as we do not want most devs interfering with it. And we can add anything we want, such as backend URLs for staging and prod, etc.
Now let’s see how our main Delegation pattern works.
Main Delegation Pattern
Here is an example build variant: