Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexey-nobody/flutter-app-environment
Simple solution to handle environment variables using `.json` or config in entrypoint file.
https://github.com/alexey-nobody/flutter-app-environment
dart dartlang flutter flutter-package flutter-plugin
Last synced: 3 months ago
JSON representation
Simple solution to handle environment variables using `.json` or config in entrypoint file.
- Host: GitHub
- URL: https://github.com/alexey-nobody/flutter-app-environment
- Owner: alexey-nobody
- License: mit
- Created: 2023-02-21T14:08:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-17T08:08:48.000Z (3 months ago)
- Last Synced: 2024-10-19T10:16:39.139Z (3 months ago)
- Topics: dart, dartlang, flutter, flutter-package, flutter-plugin
- Language: Dart
- Homepage: https://pub.dev/packages/flutter_app_environment
- Size: 60.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Flutter App Environment
A simple solution to manage environment variables using `.json` files or configuration in the entrypoint file.---
## Links
- See [CHANGELOG.md](./CHANGELOG.md) for major/breaking updates.
- Check out the [Example](./example/) for a detailed explanation of all features.---
## Installation
To install the package, run the following command:
```sh
flutter pub add --dev flutter_app_environment
```---
## Managing Environment Variables from a `.json` File
### Requirements
Before initializing the environment, ensure the following:
```dart
WidgetsFlutterBinding.ensureInitialized();
```Add the configuration files path to **pubspec.yaml**:
```yaml
flutter:
assets:
- res/config/
```- For **EnvironmentType.development**, use `development.json` as the configuration file.
- For **EnvironmentType.test**, use `test.json` as the configuration file.
- For **EnvironmentType.production**, use `production.json` as the configuration file.### Usage: Three Simple Steps
1. **Create the Config**
```dart
@JsonSerializable(createToJson: false)
class EnvironmentConfig {
const EnvironmentConfig({
required this.title,
required this.initialCounter,
});factory EnvironmentConfig.fromJson(Map json) =>
_$EnvironmentConfigFromJson(json);final String title;
final int initialCounter;
}
```2. **Initialize the Environment**
```dart
WidgetsFlutterBinding.ensureInitialized();await Environment.initFromJson(
environmentType: EnvironmentType.development,
fromJson: EnvironmentConfig.fromJson,
);
```3. **Use the Config**
```dart
home: HomePage(
title: Environment.instance().config.title,
),
```---
## Managing Environment Variables from the Entrypoint File
### Usage: Three Simple Steps
1. **Create the Config**
(same as the previous example)2. **Initialize the Environment**
```dart
WidgetsFlutterBinding.ensureInitialized();Environment.init(
environmentType: EnvironmentType.test,
config: const EnvironmentConfig(
title: 'Test environment title',
initialCounter: 0,
),
);
```3. **Use the Config**
```dart
home: HomePage(
title: Environment.instance().config.title,
),
```---
## Managing Environment Variables with Custom Types
1. **Create a Custom Environment Type**
```dart
enum CustomEnvironmentType { dev, stage, prod }
```2. **Initialize with a JSON Config**
```dart
await Environment.initFromJsonWithCustomType(
environmentType: CustomEnvironmentType.stage,
fromJson: EnvironmentConfig.fromJson,
);
```3. **Initialize from the Entrypoint File**
```dart
Environment.initWithCustomType(
environmentType: CustomEnvironmentType.dev,
config: const EnvironmentConfig(
title: 'Custom environment title',
initialCounter: 0,
),
);
```---
### Example Project Structure
For a better understanding, here's an example of how your project structure might look when handling environment variables:
```
your_project/
│
├── res/
│ └── config/
│ ├── development.json
│ ├── test.json
│ └── production.json
│
├── lib/
│ ├── main.dart
│ └── environment_config.dart
│
└── pubspec.yaml
```In this example, JSON configuration files are stored in the `res/config/` folder and declared in `pubspec.yaml`. The environment configuration is loaded in `main.dart` and used throughout the app.
---
### Notes
- Make sure to call `WidgetsFlutterBinding.ensureInitialized()` before initializing the environment.
- The `EnvironmentConfig` class should match the structure of your `.json` files or entrypoint configurations.
- You can switch between different environment types depending on your build (development, test, production) or create custom environment types.---
## Contribute
Feel free to fork, improve, submit pull requests, or report issues. I’ll be happy to fix bugs or enhance the extension based on your feedback.
---
### License
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more details.