Last updated:
0 purchases
flutter app environment
Flutter App Environment #
Simple solution to handle environment variables using .json or config in entrypoint file.
Links #
See CHANGELOG.md for major/breaking updates
Example with explain all features
Installation #
$ flutter pub add --dev flutter_app_environment
copied to clipboard
Requirements for handle environment variables from .json config #
Call before initialize the environment
WidgetsFlutterBinding.ensureInitialized();
copied to clipboard
Add res/config/ to pubspec.yaml assets. This folder contains json files with environment variables
flutter:
assets:
- res/config/
copied to clipboard
For EnvironmentType.development use name development.json for configuration file
For EnvironmentType.test use name test.json for configuration file
For EnvironmentType.production use name production.json for configuration file
Usage for handle environment variables from .json config #
Easy three steps #
Create config
@JsonSerializable(createToJson: false)
class EnvironmentConfig {
const EnvironmentConfig({
required this.title,
required this.initialCounter,
});
factory EnvironmentConfig.fromJson(Map<String, dynamic> json) =>
_$EnvironmentConfigFromJson(json);
final String title;
final int initialCounter;
}
copied to clipboard
Initialize
WidgetsFlutterBinding.ensureInitialized();
await Environment.initFromJson<EnvironmentConfig>(
environmentType: EnvironmentType.development,
fromJson: EnvironmentConfig.fromJson,
);
copied to clipboard
Use it
home: HomePage(
title: Environment<EnvironmentConfig>.instance().config.title,
),
copied to clipboard
Usage for handle environment variables from entrypoint file #
Easy three steps #
Create config
@JsonSerializable(createToJson: false)
class EnvironmentConfig {
const EnvironmentConfig({
required this.title,
required this.initialCounter,
});
factory EnvironmentConfig.fromJson(Map<String, dynamic> json) =>
_$EnvironmentConfigFromJson(json);
final String title;
final int initialCounter;
}
copied to clipboard
Initialize
WidgetsFlutterBinding.ensureInitialized();
Environment.init<EnvironmentConfig>(
environmentType: EnvironmentType.test,
config: const EnvironmentConfig(
title: 'Test evironment title',
initialCounter: 0,
),
);
copied to clipboard
Use it
home: HomePage(
title: Environment<EnvironmentConfig>.instance().config.title,
),
copied to clipboard
Usage for handle environment with custom environment type #
Create environment type
enum CustomEnvironmentType { dev, stage, prod }
copied to clipboard
Use method initFromJsonWithCustomType for json config
await Environment.initFromJsonWithCustomType<EnvironmentConfig,
CustomEnvironmentType>(
environmentType: CustomEnvironmentType.stage,
fromJson: EnvironmentConfig.fromJson,
);
copied to clipboard
Or use method initWithCustomType for entrypoint config
Environment.initWithCustomType<EnvironmentConfig, CustomEnvironmentType>(
environmentType: CustomEnvironmentType.dev,
config: const EnvironmentConfig(
title: 'Test evironment title',
initialCounter: 0,
),
);
copied to clipboard
Contribute #
Please feel free to fork, improve, make pull requests or fill issues.
I'll be glad to fix bugs you encountered or improve the extension.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.