flutter_environment
Installation
- Add this to your packages pubspec.yaml file:
dependencies:
flutter_environment: <^last>
- Install it You can install it from the command line:
$ flutter pub get
- Import it Now in Dart code, you can use:
import 'package:flutter_environment/flutter_environment.dart';
Usage
Setup flutter_environment in your main function, we recommend you to set it at the top of your main to be able to use Environment.instance in any location of your app.
void main() {
EnvironmentType environmentType = EnvironmentConfig.environmentType;
Environment(
environmentType: environmentType,
color: Colors.red.shade900,
values: EnvironmentValues(
environmentType == EnvironmentType.dev
? 'https://www.duckma.org'
: 'https://www.duckma.com',
),
);
}
To set value based on environmentType
, use Environment.instance.environment
and use a switch
like:
String _getSomeValue(EnvironmentType environmentType) {
switch (environmentType) {
case EnvironmentType.dev:
case EnvironmentType.alpha:
return 'Users angry';
case EnvironmentType.beta:
case EnvironmentType.prod:
return 'Users happy';
}
}
NOTE: This not possible for the EnvironmentValues.baseUrl
since Environment
is not initialized
yet, so we make use of EnvironmentConfig.environmentType
.
If you need to have more EnvironmentValues
create an extension on it, we do this because singleton
with static getter cannot deal with type paramiters
.
extension EnvironmentValuesE on EnvironmentValues {
String get envKey => 'DEV';
String get userStatus => _getSomeValue(Environment.instance.environment);
}
Getting info
You can get information of the current EnvironmentType
by using:
print('Is development environment: ${Environment.isDevelopment}');
print('Is alpha environment: ${Environment.isAlpha}');
print('Is beta environment: ${Environment.isBeta}');
print('Is production environment: ${Environment.isProduction}');
class Api {
final baseUrl = Environment.instance.values.baseUrl;
}
Enjoy!