Flutter production and env file

Created At: 2024-04-08 16:28:05 Updated At: 2024-04-09 03:54:06

Here we will see how to set up Flutter production environment using .env files and how they are concerned with security. 

At the same time we will see how a package called flutter_dotenv helps us. 

flutter_dotenv: ^5.1.0

Here the package flutter_dotenv package helps us maintain our .env files and retreive information.

In the project root folder you need to create .env.development and .env.production file. They should contain information like app related keys, secrets and baseUrl. There could be more information like database info, google map info.

These two files we need to load as assets. So in our pubspec.ymal file, we need to mention where to find them.

Here you see we have mentioned the files name's under assets

Now let's see what should our .env files look like. 

You may put all the info that are important and you don't wanna share with others. And this is very important for security. Since we don't share .env files with others through code sharing system like Github or others, then these info are just for yourself.

You must mention your .env files in your gitingore module.

Since now we know what to put in our .env files. Now how to read them back and read correctly? Remember we have installed a package? That package will come in rescue to load the information from .env file to Flutter app memory.

We have created Environment class to load the .env file information. Here dotenv.env is coming from our installed package. This dotenv.env file can read all the information from the .env file.

Now the last step is tell Flutter to load the .env file using flutter_dotenv package. 

Here inside main() you may the second line. We are loading and reading the file. You may get the code from the top right.

Comment

Add Reviews