flutter_app_environment 1.0.0 copy "flutter_app_environment: ^1.0.0" to clipboard
flutter_app_environment: ^1.0.0 copied to clipboard

Simple solution to handle environment variables using `.json` or config in entrypoint file.

Flutter App Environment #

Simple solution to handle environment variables using .json or config in entrypoint file.


Installation #

$ flutter pub add --dev flutter_app_environment

Requirements for handle environment variables from .json config #

  • Call before initialize the environment

    WidgetsFlutterBinding.ensureInitialized();
    
  • Add res/config/ to pubspec.yaml assets. This folder contains json files with environment variables

    flutter:
        assets:
            - res/config/
    
  • 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 #

  1. 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;
    }
    
  2. Initialize

    WidgetsFlutterBinding.ensureInitialized();
    
    await Environment.initFromJson<EnvironmentConfig>(
        environmentType: EnvironmentType.development,
        fromJson: EnvironmentConfig.fromJson,
    );
    
  3. Use it

    home: HomePage(
        title: Environment<EnvironmentConfig>.instance().config.title,
    ),
    

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.

4
likes
0
pub points
69%
popularity

Publisher

unverified uploader

Simple solution to handle environment variables using `.json` or config in entrypoint file.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, json_annotation

More

Packages that depend on flutter_app_environment