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.

Well, Flutter with its “foundations” package gives us with following constants:

  1. kReleaseMode
  2. kProfileMode
  3. 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:

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:

Debug Mode
Release Mode

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

Github: https://github.com/retroportalstudio

Happy Coding! ✌😁

Mobile Application and Web Developer | @Youtube Content Creator | Worship #reactjs #flutter #java #dart | youtube.com/retroportalstudio