How dart-define can help you on your Flutter project?

Thiago Evoá
3 min readJun 14, 2021

Hello dev, have you ever had the necessity to run your Flutter project with specific values according the environment you deployed the app, and how did you store for example a distinct value on a dev or production app? If you have used a local property file and want to abstract even more this such thing from your project keep reading this article.

If you came from a backend development maybe you already used args to run your project and this is the same thing here in the Flutter project, so let’s take a look in the example below to see how it’s going to work.

After create the project go to the MyApp widget and add this String.fromEnviroment(‘APP_ENV’) in the title like the image below.

APP_ENV

The next step is create the lunch.json file, go to the Run and Debug view on your Visual Studio Code and click on the create a launch.json file option as the image below.

launch.json

Now that you have your launch.json file created copie the code as the image below to your file.

configuration

And now, run the project pushing the play button on the top left of this view. and you will have something like the image below.

simulator

Now lets create another configuration with the APP_ENV value as PROD like the image below, and to run this new configuration just change the option beside the play button.

simulator

Before we finish the article, if you want to build the project in the terminal just run the command “flutter build apk --debug --dart-define=APP_ENV=DEV ” for example.

Here it is, with this simple example you could see that you don’t have to store any configuration inside a property file, because you can put these configurations in your CI/CD script and never worries about expose it and still have the flexibility of build to different environments easily.

--

--