Code Monkey home page Code Monkey logo

envied's Introduction

ENVied

Pub CI codecov Melos License: MIT

A cleaner way to handle your environment variables in Dart/Flutter.

(GREATLY inspired by Envify)


Table of Contents


Overview

Using a .env file such as:

KEY=VALUE

or system environment variables such as:

export VAR=test

and a dart class:

import 'package:envied/envied.dart';

part 'env.g.dart'

@Envied()
abstract class Env {
    @EnviedField(varName: 'KEY')
    static const key = _Env.key;
}

Envied will generate the part file which contains the values from your .env file using build_runner

You can then use the Env class to access your environment variable:

print(Env.key); // "VALUE"

Install

Add both envied and envied_generator as dependencies,

If you are using creating a Flutter project:

$ flutter pub add envied
$ flutter pub add --dev envied_generator
$ flutter pub add --dev build_runner

If you are using creating a Dart project:

$ dart pub add envied
$ dart pub add --dev envied_generator
$ dart pub add --dev build_runner

This installs three packages:

  • build_runner, the tool to run code-generators
  • envied_generator, the code generator
  • envied, a package containing the annotations.

Usage

Add a .env file at the root of the project. The name of this file can be specified in your Envied class if you call it something else such as .env.dev.

KEY1=VALUE1
KEY2=VALUE2

Create a class to ingest the environment variables (lib/env/env.dart). Add the annotations for Envied on the class and EnviedField for any environment variables you want to be pulled from your .env file.

IMPORTANT! Add both .env and env.g.dart files to your .gitignore file, otherwise, you might expose your environment variables.

// lib/env/env.dart
import 'package:envied/envied.dart';

part 'env.g.dart';

@Envied(path: '.env.dev')
abstract class Env {
    @EnviedField(varName: 'KEY1')
    static const key1 = _Env.key1;
    @EnviedField()
    static const KEY2 = _Env.KEY2;
    @EnviedField(defaultValue: 'test_')
    static const key3 = _Env.key3;
}

Then run the generator:

# dart
dart run build_runner build
# flutter
flutter pub run build_runner build

You can then use the Env class to access your environment variables:

print(Env.key1); // "VALUE1"
print(Env.KEY2); // "VALUE2"

Obfuscation

Add the ofuscate flag to EnviedField

@EnviedField(obfuscate: true)

License

MIT © Peter Cinibulk

envied's People

Contributors

cmengler avatar daiki1003 avatar gwanhyeong avatar kaankoken avatar petercinibulk avatar techouse avatar thexxturboxx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.