Flutter_config

flutter_config

flutter_config is a Flutter package that allows you to easily configure and access your app’s environment-specific configuration variables.

Installation

To use flutter_config, you need to add the package to your pubspec.yaml file:

  dependencies:
    flutter_config: ^1.0.0
  

Then, run the following command to fetch the package:

  flutter pub get
  

Configuration

After installing the package, you can configure your environment-specific variables in the .env file at the root of your Flutter project. The .env file should have the following structure:

  
    VARIABLE_NAME=variable_value
    ANOTHER_VARIABLE=another_value
  
  

You can define as many variables as needed in this file.

Usage

To access the variables defined in the .env file, you can use the FlutterConfig class. First, import it in your Dart file:

  import 'package:flutter_config/flutter_config.dart';
  

Then, you can access the variables as follows:

  String myVariable = FlutterConfig.get('VARIABLE_NAME');
  

where ‘VARIABLE_NAME’ is the name of the variable defined in the .env file.

Here’s an example:

  
    // .env file
    API_URL=https://api.example.com
  
  
  
    // main.dart file
    import 'package:flutter/material.dart';
    import 'package:flutter_config/flutter_config.dart';

    void main() async {
      await FlutterConfig.loadEnvVariables(); // Load the environment variables

      String apiUrl = FlutterConfig.get('API_URL'); // Access the variable

      runApp(MyApp(apiUrl: apiUrl));
    }

    class MyApp extends StatelessWidget {
      final String apiUrl;

      MyApp({required this.apiUrl});

      // Rest of the code
    }
  
  

In this example, the ‘API_URL’ variable is accessed and used in the main.dart file. This allows you to have different configurations for different environments, such as development, staging, and production.

Leave a comment