App Env
With App env you can now easily share global variables between Flutter and mobile Android and iOS code.
Supported platforms
- Android
- iOS
About
Before the package appears, when you want to use the environment, you write all the variables in the .env
file, and then write them again for both systems, either in strings.xml for the Android system, or in the xcconfig file to use xCode and for the iOS system.
This matter costs you a lot of time, but also you repeat more and more data, and therefore the error rate increases.
But rest assured that App env came to solve this problem with ease .. All you have to do is only 4 steps ..
- Add the package in your project.
- Run the package.
- Add your variables in the
.env
file. - Rerun the package again.
And Boooom, now you will notice that the package has done everything, and you can share all the variables now without worrying about forgetting anything.
Usage
As we mentioned previously, there are four simple steps that will enable you to share your variables with ease. We will explain them in detail below.
1- You have added the package to your project < Package add code >
app_env: current_version
2- You can run the package using the following command
dart run app_env
3- You will notice that the package has added an .env
file in the project at the same level as the lib folder.
And when you open the file, you will find a group of variables present as an illustration. You can delete them and add your own variables ๐
4- Use the above command to run the package again.
Itโs over.. thatโs it.
Flutter Usage
At the present time, the package does not provide the ability to read the .env
file, but there are many ways to read the file, either through
- Use the Regex and write your own code to extract the key and the value.
- Or, you can easily use another package that reads the
.env
file, such as flutter_dotenv.
Android Usage
After executing the previous steps, go to the following path android/app/src/main/res/values/
You will notice that there is a strings.xml file.
When you open the file, you will find all the variables that you defined in the .env
file, and the package added them automatically.
And every time you add a new variable or modify an existing file and run the package, it will automatically modify the strings.xml file.
Thus, you can use the variables in the strings.xml file in everything related to Android, whether it is an xml file, as well as java or Kotlin files.
iOS Usage
After executing the previous steps, go to the following path ios/Flutter/
You will notice that there is a AppEnvConfig.xcconfig file.
When you open the file, you will find all the variables that you defined in the .env
file, and the package added them automatically.
All we have to do is create the AppEnvConfig.xcconfig file in the following path as shown in the image below.
To create the file, we must open the Xcode, add a new file, and as show in the photo below.You must follow two steps
- the name of file must be AppEnvConfig.xcconfig
- the path of file must be ios/Flutter/AppEnvConfig.xcconfig
There remains a very simple matter for us so that you can use these variables with ease in iOS .. All you have to do is open Runner as shown in the picture ..
The configuration file must be changed from here to the file that we created now
Through Configurations, you will choose the AppEnvConfig.xcconfig file, and then you can now easily go to the Build Settings tab located in the Targets as shown in the picture.
And then search for User to access User-Defiend to find that all the variables that we added in the .env
file are present and available now through xCode, and you can deal with them easily in iOS now.
Use Cases
Here we will perform some experiments to answer some of the questions in your mind.
- What happens if I add a variable in the strings.xml file or in the AppEnvConfig.xcconfig file that is not in the .env file and run the package?
โ strings .xml: Nothing will happen, everything will be fine.. The package does not rewrite the file from the beginning, but only modifies it, and therefore it will modify the file and all your other variables will be fine.
โ AppEnvConfig.xcconfig: As for here, the package does not modify the file, but overrides it every time with all the data in the .env file only, and therefore any variables that are not in the .env
file will be deleted.
- What will happen if I change the value of a variable in strings.xml only without changing it in the
.env
file?
The package will write the value in .env and will not care about the value you wrote. ๐
This section will be updated as more cases become available.
Discussion
Use the issue tracker for bug reports and feature requests. Pull requests are welcome.