Check for Debug/Release Mode in Flutter Apps đŻ
One of the main reasons for Flutter being so popular is its capacity to make complex tasks simple. Check your appâs build mode is one of those things which developers need in almost every app. We need it to dynamically set the API Endpoints for Debug and Release mode, Block some features which are under development, and even modify the analytics based on the mode we are using.
So, how exactly can we do that in Flutter? đ¤
Well, Flutter with its âfoundationsâ package gives us with following constants:
- kReleaseMode
- kProfileMode
- kDebugMode
We can use these three constants to detect the mode around which we want our code to be dynamic. In most cases, you would want to use the âkReleaseModeâ constant. This is only set to true when the application is built using the â release flag. Letâs take a look at this with an example:
Example:
Here is a code for a URLController class that we can use to define API endpoints and respective routes for various requests:
In this, you can see that we have two endpoints â_debugEndpointâ which we have to use for development purposes, and â_productionEndpointâ which we have to use for the production build of the app. So to choose between these we create a third constant and for its value, we use the kReleaseMode constant from the foundations package (imported on top) to check if our app is running in the release mode, and based on that we set the value of _endpoint. Then we use the _endpoint as an API Endpoint for our request routes namely login and profile.
Now if we run the app to display the login URL with the following code:
return Scaffold(
appBar: AppBar(
title: Text("Mode"),
),
body: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 50.0),
child: Text("${URLController.login}",style: TextStyle(fontSize: 25),),
),
),
);
We see the following results:
And thatâs it! đ¤Ż
It is that easy to detect the mode of Flutter Apps!
If you like to have a visual representation for reference, check out this tutorial on my Youtube Channel and youâll have a good idea! And while youâre there, if you find this useful, consider Subscribing and Sharing!
If you find this Useful, Consider Subscribing to RetroPortal Studio on Youtube at:
Here are the links to my other Social Media Handles:
Youtube: https://www.youtube.com/retroportalstudio
Twitter: https://www.twitter.com/theretroportal
Instagram: https://www.instagram.com/retroportalstudio
LinkedIn: https://www.linkedin.com/in/parasjainrps
Happy Coding! âđ