flavorz 1.0.5 copy "flavorz: ^1.0.5" to clipboard
flavorz: ^1.0.5 copied to clipboard

Generate Different Environments For Your Application From A Single Json File. With Pure Dart.

Flavors 🍧 #

This package will ease how you manage your app Flavors(Environments).

Getting Started #

Make sure you are running on Dart version 2.17.0 or later.

Add this to your package's pubspec.yaml file #

  • Add flavorz & build_runner to your dev_dependencies.
dev_dependencies:
  build_runner: ^2.2.0
  flavorz: ^1.0.5

Run dart pub get or flutter pub get to install the packages.

How to Use #

The are 2 files that require your attention:

  • .flavorz.json File
  • .flavorz.dart File

None of the above files exist in your project yet. We will create the .flavorz.json file manullay. And the .flavorz.dart file will be generated by command.

Create .flavorz.json File #

The .flavorz.json file holds the configurations for all of your falvors(environments). And it must be placed anywhere inside the lib folder.

We have to create a new file under the lib folder and name it like this name.flavorz.json, the name can be anything. but the extension must be the same.

So, let's create an example file and will name it env.flavorz.json. Inisde the json file there must be 2 attributes:

  • 'default'
  • 'environments'

'default' is a string value that should hold the name of the flavor(environment) that we want to set as default.

'environments' is a list of configuration for each flavor. And the each falvor must have the following attribute:

  • _name

You can add as many flavors as you want in the environments list. And you can add as many attributes as you want after the _name.

Notes For The Json File

  • You should never remove the '_name' attribute
  • It is preferable to use camelCase format for your attribute names
  • Attributes that start with an underscore will be generated as private

Following is a sample of how the file should look like:

{
    "default": "dev",
    "environments": [
        {
            "_name": "dev",
            "versionNumber": "Dev 1.0.0",
            "camelCaseAttribute": ""
        },
        {
            "_name": "local",
            "versionNumber": "Local 1.0.1",
            "camelCaseAttribute": ""
        }
    ]
}

As you can see, the 'environments' list holds all different flavors for your app. e.g Dev, Prod, Mock.

Run The Builder #

In your terminal, run dart run build_runner build or flutter pub run build_runner build to generate the environment file.

After running the command, a new file will be generated. The generated file will have the same name & path of the .flavorz.json file from previous step. And its extension will be .flavorz.dart

The generated file will contain the Environment class that we can use across our app.

Start Using The Environment Class #

You can access your environment data from anywhere in your application using the factory Environment() after importing the file, as seen below.

import 'env.flavorz.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final type = Environment().type;
  print(type); // prints 'dev'
}

Run Your App #

To run your app with a specific environment, run the following command flutter run --dart-define="env=dev". Change dev with the name of the evnironment you want to run.

If you just run flutter run then it will run the default environment as defined in the .flavorz.json file.

NOTES #

  • You must run the build_runner whenever you have modify the .flavorz.json file.
5
likes
160
points
62
downloads

Publisher

verified publisheraymanz.dev

Weekly Downloads

Generate Different Environments For Your Application From A Single Json File. With Pure Dart.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

build, flutter

More

Packages that depend on flavorz